# category.py


from django.db.models.fields import CharField


class CategoryField(CharField):
    verbose_name = "category"
    max_length = 40

    NORMAL = 'normal'
    PUBLIC = 'public'

    choices = (
        (NORMAL, "normal"),
        (PUBLIC, "public),
    )

    def __init__(self, *args, **kwargs):
        kwargs['max_length'] = self.max_length
        kwargs['choices'] = self.choices
        kwargs['verbose_name'] = self.verbose_name
        kwargs['blank'] = True
        kwargs['null'] = True
        kwargs['default'] = self.NORMAL
        super().__init__(*args, **kwargs)



# in my models.py

class Car(models.Model):

    category = CategoryField()




When I use custom field overriding django CharField, field changes that 
`max_length`, `choices` not detected by django makemigrations command.

Add field migration works fine, and verbose_name, blank, null changes are 
detected by makemigrations command.
However, when I change max_length and choices, django makemigrations cannot 
detect changes.

I checked return value of my CategoryField's deconstruct method, and the 
result is fine.
Also, if max_length is set like `CategoryField(max_length=50)`, 
makemigrations can detect changes.

Why django makemigrations cannot detect changes when set max_length and 
choices in __init__ method?







-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/a4512f2c-88a9-44fd-a616-5538dacf5534%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to