-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Peewee calls __init__ on model unexpectedly #856
Comments
Try modifying your
|
@lez is correct. Models that override |
Thanks. I guess my confusion came from the fact this used to work fine on earlier versions. Might be worth a mention in the documentation about not overriding |
Same with me. I overrode init() and it used to work, but I believe I upgraded to a newer version of peewee and it now spits out the following error: TypeError: init() got an unexpected keyword argument 'database_column_name' |
@coleifer so what is the right way to init the data fields of peewee.Model descendant class? |
A |
thanks @coleifer! suppose I have a Car class that inherits from peewee.Model. If I want to initialise an object of the Car class with individual values, I can just use the constructor:
So far, so good! But now suppose I want to initialise the Car object, starting from another object, say some CarConfig object and the initialisation requires some logic, more than just assigning fields 1-to-1. Without peewee, I would simply define my __init__() method and put all the initialisation logic there
but I can't define the __init__() method in peewee.Model descendant classes anymore. |
You can still put it in |
good idea - I think the factory method is the way to go! |
Could someone share an example of how would that factory method look like ? |
This is just a way to avoid overriding |
Since |
I would not advise it unless you're quite familiar with all the differences between class User(Model):
username = TextField()
def __init__(self, *args, **kwargs):
super(User, self).__init__(*args, **kwargs) # Ensure parent init is called correctly.
# do complicated stuff here. But my advice is to probably stick to a classmethod constructor if your logic is particularly complex. |
We've run into some trouble when upgrading to peewee 2.8.0, when we have a custom
__init__
defined on our models. This behaviour worked fine when on earlier versions (2.7.3 is the other version I tested).In the code above, the failure occurs in the final select, it seems that when the model instance is created, the code in
playhouse._speedups._ModelQueryResultWrapper.process_row
uses__init__
to create an instance of the row with the full results of the query passed as kwargs (i.e. a=2, b=2). However, theTestTable2.__init__
does not supportb
as a keyword argument.It's not clear from the documentation how well I should expect custom
__init__
methods to work. However, I would imagine that this could be fixed by makingplayhouse._speedups._ModelQueryResultWrapper.process_row
explicitly callpw.Model.__init__
on an instance created via__new__
.The text was updated successfully, but these errors were encountered: