4👍
✅
You might be looking for one of these batch operations:
3👍
You can use first_or_none to check if a node exists.
Example:
person = Person.nodes.first_or_none(name='James')
if person == None:
personNew = Person(name='James').save()
- [Django]-How to prevent duplicate entries in model from django admin
- [Django]-Django meta permissions
2👍
I have created my own query to do that. Check if it helps.
def exists(node=None, property=None, value=None):
filter_node = (":" + node) if node != None else ''
filter_value = ("{" + property + ": '" + value + "'}") if property != None and value != None else ''
return db.cypher_query("MATCH(n" + filter_node + filter_value + ")" + " return count(n) > 0 as exists;" )[0][0][0]
exists(node='User')
- [Django]-Django: How can I implement Hierarchy of classes in Django?
- [Django]-Keep User and Group in same section in Django admin panel
- [Django]-Django 1.1 equivalent of the 'in' operator
0👍
bit extending on @raman Kishore’s answer, I did this, cause I wanted to check if object’s exists and also, sometimes I would insert an ID for myself because it came from another DB and I wanted to keep compatibility, so did the following:
class DefaultMixin:
date_added = DateTimeProperty()
nodes: NodeSet # makes pycharm smarter
def exists(self, **filters):
if not filters:
if not self.id:
raise KeyError('no filters or id provided')
filters['id'] = self.id
return bool(self.nodes.first_or_none(**filters))
- [Django]-UnicodeEncodeError raised when 'createsuperuser' is called
- [Django]-Django: Store Hierarchical Data
- [Django]-Django collectstatic on S3 uploads unchanged files everytime on Heroku
- [Django]-How make conditional expression on Django with default value of object?
0👍
I created a decorator over each of my StructuredNode classes, which checks for the node upon initialization.
def check_if_node_exists(node):
def node_checker(**kwargs):
result = node.nodes.first_or_none(**kwargs)
if result == None:
print("Creating New Node")
return node(**kwargs).save()
print("Returning Existing Node")
return result
return node_checker
@check_if_node_exists
class Employee(StructuredNode):
name = StringProperty()
To call it, just create an instance of the class:
employee1 = Employee(
name = "test_name"
)
- [Django]-Django celery: Import error – no module named task
- [Django]-Web Dev. Framework for RAD Web App Dev. in Shortest Time ( Yii vs. Django )
- [Django]-Postgres with Django – Improperly configured. Error : ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'
- [Django]-Using a static website generator for a blog on a dynamic website?
- [Django]-POST API response blocked by CORS policy – React and Django Rest Framwork
Source:stackexchange.com