Writing a __init__ function to be used in django model
Solution 1:
Relying on Django's built-in functionality and passing named parameters would be the simplest way to go.
p = User(name="Fred", email="[email protected]")
But if you're set on saving some keystrokes, I'd suggest adding a static convenience method to the class instead of messing with the initializer.
# In User class declaration
@classmethod
def create(cls, name, email):
return cls(name=name, email=email)
# Use it
p = User.create("Fred", "[email protected]")
Solution 2:
Django expects the signature of a model's constructor to be (self, *args, **kwargs)
, or some reasonable facsimile. Your changing the signature to something completely incompatible has broken it.
Solution 3:
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