Django makemigrations cannot detect change when changing custom field overriding charfield.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Django makemigrations cannot detect change when changing custom field overriding charfield.

김지환
# 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 [hidden email].
To post to this group, send email to [hidden email].
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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Django makemigrations cannot detect change when changing custom field overriding charfield.

Tim Graham-2
It looks like you need to add a CategoryField.deconstruct() method.

https://docs.djangoproject.com/en/stable/howto/custom-model-fields/#custom-field-deconstruct-method

On Monday, March 13, 2017 at 8:05:02 AM UTC-4, 김지환 wrote:
# 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 [hidden email].
To post to this group, send email to [hidden email].
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/2894bc7f-d561-4ae1-96d8-c2c2203cab7d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Django makemigrations cannot detect change when changing custom field overriding charfield.

김지환
CategoryField subclassed CharField, so I think I don't need to add deconstruct method in CategoryField.

And, when call CategoryField().deconstruct(), the values return correctly.

Why django makemigrations cannot detect max_length and choices change?




2017년 3월 13일 월요일 오후 10시 43분 53초 UTC+9, Tim Graham 님의 말:
It looks like you need to add a CategoryField.deconstruct() method.

<a href="https://docs.djangoproject.com/en/stable/howto/custom-model-fields/#custom-field-deconstruct-method" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdocs.djangoproject.com%2Fen%2Fstable%2Fhowto%2Fcustom-model-fields%2F%23custom-field-deconstruct-method\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFb4jMtpEa9bFxmlLITvYu-8U0ESQ&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdocs.djangoproject.com%2Fen%2Fstable%2Fhowto%2Fcustom-model-fields%2F%23custom-field-deconstruct-method\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFb4jMtpEa9bFxmlLITvYu-8U0ESQ&#39;;return true;">https://docs.djangoproject.com/en/stable/howto/custom-model-fields/#custom-field-deconstruct-method

On Monday, March 13, 2017 at 8:05:02 AM UTC-4, 김지환 wrote:
# 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 [hidden email].
To post to this group, send email to [hidden email].
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/3dcb255e-247c-4849-824f-ac04edf44469%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Django makemigrations cannot detect change when changing custom field overriding charfield.

Tim Graham-2
CategoryField.__init__() has lines like kwargs['max_length'] = self.max_length

That's going to ignore any custom max_length that you provide in the model field's definition, hence those changes aren't detected in migrations.

On Tuesday, March 14, 2017 at 2:09:12 AM UTC-4, 김지환 wrote:
CategoryField subclassed CharField, so I think I don't need to add deconstruct method in CategoryField.

And, when call CategoryField().deconstruct(), the values return correctly.

Why django makemigrations cannot detect max_length and choices change?




2017년 3월 13일 월요일 오후 10시 43분 53초 UTC+9, Tim Graham 님의 말:
It looks like you need to add a CategoryField.deconstruct() method.

<a href="https://docs.djangoproject.com/en/stable/howto/custom-model-fields/#custom-field-deconstruct-method" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdocs.djangoproject.com%2Fen%2Fstable%2Fhowto%2Fcustom-model-fields%2F%23custom-field-deconstruct-method\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFb4jMtpEa9bFxmlLITvYu-8U0ESQ&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdocs.djangoproject.com%2Fen%2Fstable%2Fhowto%2Fcustom-model-fields%2F%23custom-field-deconstruct-method\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFb4jMtpEa9bFxmlLITvYu-8U0ESQ&#39;;return true;">https://docs.djangoproject.com/en/stable/howto/custom-model-fields/#custom-field-deconstruct-method

On Monday, March 13, 2017 at 8:05:02 AM UTC-4, 김지환 wrote:
# 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 [hidden email].
To post to this group, send email to [hidden email].
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/b900960b-272f-49e7-898f-72193f0866ab%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...