11

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?

1
  • 1
    What do you want this attribute to do exactly? Commented Dec 19, 2013 at 10:34

5 Answers 5

6

If you check CharField

class CharField(Field):
    description = _("String (up to %(max_length)s)")

    def __init__(self, *args, **kwargs):
        super(CharField, self).__init__(*args, **kwargs)
        self.validators.append(validators.MaxLengthValidator(self.max_length))

It accepts **kwargs but also calls super and inherited __init__ (Field object) only accepts named parameters.

class Field(object):
    def __init__(self, verbose_name=None, name=None, primary_key=False,
        max_length=None, unique=False, blank=False, null=False,
        db_index=False, rel=None, default=NOT_PROVIDED, editable=True,
        serialize=True, unique_for_date=None, unique_for_month=None,
        unique_for_year=None, choices=None, help_text='', db_column=None,
        db_tablespace=None, auto_created=False, validators=[],
        error_messages=None):

So you can not pass a custom argument.

You must create a custom field

Sign up to request clarification or add additional context in comments.

Comments

5

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)

Comments

1

If you take a look at /django/db/models/fields/init.py, you can see that such behavior is not intended as Field.init accepts only predefined arguments.

You are, of course, free to write your own custom fields (https://docs.djangoproject.com/en/dev/howto/custom-model-fields/).

Comments

1

This worked for me:

class SpecialCharField(models.CharField):
    def __init__(self, *args, **kwargs):
        self.aaaaa=kwargs.pop('aaaaa',false)
        super(SpecialCharField,self).__init__(*args, **kwargs)


class ModelXY(models.Model):
    charField = SpecialCharField('char field', max_length=1024, aaaaa=true)

1 Comment

You provably prefer to use kwargs.pop('aaaaa') instead of retrieving with .get and then creating a new kwargs object.
0

You failed to mention what you want this attribute to actually do.

Generally speaking, if you're trying to customize the CharField behavior, and it's not something CharField provides out-of-the-box, you'll need to create your own CustomCharField that extends models.CharField and implements the behavior you're expecting from aaaaaaa=True.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.