Adding custom attributes to django model field

Viewed 10882

Can I add some arbitrary attributes to django model field?

For example:

charField = models.CharField('char field', max_length=1024, aaaaa=true)

Can I add attribute aaaaa?

Is this doable?

5 Answers

You can assign it as attribute to field object itself:

charField = models.CharField('char field', max_length=1024)
charField.aaaaa = True

Or if you want one liner create a function e.g.:

def extra_attr(field, **kwargs):
  for k, v in kwargs.items():
    setattr(field, k, v)
  return field

and then use:

charField = extra_attr(models.CharField('char field', max_length=1024), aaaaa=True)
Related