Opened 5 years ago

Last modified 5 years ago

#30857 closed Bug

When subclassing a model field, `max_length` cannot be passed as an argument to `self.__init__` — at Initial Version

Reported by: Robin (Robert) Thomas Owned by: nobody
Component: Documentation Version: 2.2
Severity: Normal Keywords: models fields
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

class MyCustomField(models.CharField):
    def __init__(self, *args, **kwargs):
        super().__init__(max_length=60, *args, **kwargs)

class MyModel(models.Model)
    address_state = MyCustomField()

On attempting to migrate, I get:

super().__init__(max_length=60, *args, **kwargs)
TypeError: __init__() got multiple values for keyword argument 'max_length'

Using kwargs[max_length] works just fine. However, the documentation says that max_length should be able to be passed to super().__init__() as an argument:

https://docs.djangoproject.com/en/2.2/howto/custom-model-fields/#writing-a-field-subclass

Either the documentation is incorrect and should be fixed, or this is a bug.

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top