Inheritance and Custom constructors
See original GitHub issueHi all,
the documentation is a bit vague about how to do define your own constructor when inheritance is being performed. The documentation is here: https://neomodel.readthedocs.io/en/latest/extending.html
The example given is:
class Item(StructuredNode):
name = StringProperty(unique_index=True)
uid = StringProperty(unique_index=True)
def __init__(self, product, *args, **kwargs):
self.product = product
kwargs["uid"] = 'g.' + str(self.product.pk)
kwargs["name"] = self.product.product_name
super(Item, self).__init__(self, *args, **kwargs)
that just doesnt work - you can run this yourself and realise that you will get an exception on StructuredNode constructor.
One can make it work with the following:
class Item(StructuredNode):
name = StringProperty(unique_index=True)
uid = StringProperty(unique_index=True)
def __init__(self, product, *args, **kwargs):
self.product = product
kwargs["uid"] = 'g.' + str(self.product.pk)
kwargs["name"] = self.product.product_name
super(Item, self).__init__(*args, **kwargs)
Still, there’s a problem when the .save() method is being called on that object. This happens because it tries to inflate an object using this constructor, without “product” argument being provided.
a solution would be:
class Item(StructuredNode):
name = StringProperty(unique_index=True)
uid = StringProperty(unique_index=True)
def __init__(self, product = None, *args, **kwargs):
if product is not None:
self.product = product
kwargs["uid"] = 'g.' + str(self.product.pk)
kwargs["name"] = self.product.product_name
super(Item, self).__init__(*args, **kwargs)
Which is quite ugly and I believe that probably not the intended implementation. How is the correct way to perform it? I can improve the docs on that if required.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6
@MarcoLotz I think that I may have misinterpreted your question. If you are not looking for advice on a specific problem you are dealing with and you are simply highlighting an issue with the documentation then you are welcome to provide a PR about it.
@MarcoLotz What do you think?