[BangPypers] ChoiceField in django models

Gora Mohanty gora at mimirtech.com
Thu Nov 10 11:26:42 CET 2011


On Thu, Nov 10, 2011 at 3:25 PM, Asif Jamadar <asif.jamadar at rezayat.net> wrote:
> Suppose I have choicefield in my django model which consist of several choices. Now in future if I changed the existing choice (option) with some other name (choice), then whether the existing records in model with that choice(option)  will also change? Or In admin page I need to set that new option manually?

No, they won't change automatically. There is no reasonable
way for the system to intuit that this change was made. You
would need to manually edit the existing object instances in
the database, either through the admin. interface, or through
a "python manage.py shell" script. Something along the lines
of:

from myapp.models import MyModel

for obj in MyModel.objects.all():
    if obj.mychoicefield == 'Old value':
        obj.mychoicefield = 'New value'
        obj.save()

Regards,
Gora


More information about the BangPypers mailing list