This works:
class MyModel(peewee.Model):
my_field = peewee.IntegerField(null=False, default=0)
class Meta(object):
database = db
db_table = 'MyTable'
This does not work:
class MyModel(peewee.Model):
class Meta(object):
database = db
db_table = 'MyTable'
setattr(MyModel, 'my_field', peewee.IntegerField(null=False, default=0))
I guess this is due to some metaclass magic done in peewee.Model
. Indeed, I can't see it in the model's _meta.fields
.
What would be a good way to define the fields dynamically?
Instead of setattr, call add_to_class
:
my_field = peewee.IntegerField(null=False, default=0)
my_field.add_to_class(MyModel, 'my_field_name')
# Now this works:
MyModel.my_field_name
This post is just to give prominence to the correct answer given by AidanGawronski for Peewee 3.? (tested 2020-05-18).
Code such as the following succeeds:
class Note(Model):
note_id = AutoField()
fields=('nx', 'ny', 'nz')
for f in fields:
Note._meta.add_field(f, TextField(null=False))
Peewee's migrate module apparently can be used to accomplish the same thing, but I haven't tried it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With