Writing a __init__ function to be used in django model

前端 未结 4 1732
情深已故
情深已故 2020-11-29 04:49

I\'m trying to write an __init__ function for one of my models so that I can create an object by doing:

p = User(\'name\',\'email\')
         


        
4条回答
  •  粉色の甜心
    2020-11-29 05:41

    The correct answer is to avoid overriding __init__ and write a classmethod as described in the Django docs.

    But this could be done like you're trying, you just need to add in *args, **kwargs to be accepted by your __init__, and pass them on to the super method call.

    def __init__(self, name, email, house_id, password, *args, **kwargs):
            super(models.Model, self).__init__(self, *args, **kwargs)
            self.name = name
            self.email = email
    

提交回复
热议问题