简体   繁体   中英

Annotate a django field

Is it possible to add some extra arguments to a field definition so I can parse that at a later time, such as:

class Item(models.Model):
    field = models.CharField(max_length=40, something='yes')

Later on:

for field_obj in Item._meta.fields[1:]: # ignore auto-pk
    field = field_obj.name
    something = field_obj.annotations['something']
    print (field, something)

You can add an attribute after defining the field

class Item(models.Model):
    field = models.CharField(max_length=40)
    field.something = 'yes'

Then it should be available on the field

Item._meta.get_field('field').something

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM