1

I'm getting this error when trying to override the StructuredNode constructor, while it is almost exact same code from the doc.

Traceback (most recent call last):
  File "/Users/xiao/PycharmProjects/Fooga_New/test/tmp.py", line 48, in <module>
    tmp_node = Item(test='test_test_test')
  File "/Users/xiao/PycharmProjects/Fooga_New/test/tmp.py", line 45, in __init__
    super(Item, self).__init__(self, *args, **kwargs)
  File "/Users/xiao/PycharmProjects/python3_venv/lib/python3.6/site-packages/neomodel/core.py", line 203, in __init__
    super(StructuredNode, self).__init__(*args, **kwargs)
TypeError: __init__() takes 1 positional argument but 2 were given

Here's my code:

from neomodel import db, StructuredNode, StringProperty


db.set_connection('bolt://' + 'neo4j' + ':' + '5428' + '@' + '192.168.0.24' + ':' + '7687')


class Item(StructuredNode):
    name = StringProperty(unique_index=True)
    uid = StringProperty(unique_index=True)

    def __init__(self, test, *args, **kwargs):
        # self.product = product
        kwargs["uid"] = 'g.' + str(test)
        kwargs["name"] = test
        super(Item, self).__init__(self, *args, **kwargs)


tmp_node = Item(test='test_test_test')
tmp_node.save()

I'm wondering if I'm using this right?

Thanks.

Xiao Yi
  • 11
  • 3
  • The `tmp_node` assignment in your stack trace does not match the one in the code you've provided (the values for `test` are clearly different). Please make sure you've provided code that definitely produces the error, and provide the exact error that code produces, otherwise we can't be sure we're looking at code that actually has the problem you're seeing. – kungphu Oct 15 '18 at 06:32
  • Sorry about that. I've made my edits. – Xiao Yi Oct 15 '18 at 14:48

1 Answers1

0

Please take a look at *args and **kwargs? before going on.

In your example you could use the test parameter in two ways:

from neomodel import db, StructuredNode, StringProperty
from neomodel import config

config.DATABASE_URL = 'bolt://neo4j:neo4j@localhost:7687'

# Using `*args`: The first parameter to the constructor defines the `test`

class Item(StructuredNode):
    uid = StringProperty(unique_index=True)
    name = StringProperty(unique_index=True)
    def __init__(self, *args, **kwargs):       
        if args:
            test = args[0]
            kwargs['uid'] = 'g.' + str(test)
            kwargs['name'] = test
            super(Item, self).__init__(**kwargs)
        else:
            print('no test')


item = Item('test_test_test')
item.save()

# or using `**kwargs`: a entry `test` in kwargs defines the `test` for uid and name

class Kitem(StructuredNode):
    uid = StringProperty(unique_index=True)
    name = StringProperty(unique_index=True)

    def __init__(self, *args, **kwargs):       
        if kwargs and 'test' in kwargs: 
            test = kwargs['test']
            kwargs.pop('test',None)
            kwargs['uid'] = 'g.' + str(test)
            kwargs['name'] = test
            super(Kitem, self).__init__(**kwargs)
        else:
            print('no test')


kitem = Kitem(test='test_test_test_k')
kitem.save()
chriopp
  • 947
  • 7
  • 12