I have a form where I ask people's hobbies. It was a Charfield in my form. I transformed this field with multiple CHOICES. For my old datas, I would like to update their values: for example if they had entered: "football", "skiing", it would have to change to 'sport'.
abstract_models.py
class ReferencesProfileBase(models.Model, UsernameNaturalKeyMixin):
HOBBY_CHOICES =
('sport', ('sport')),
('music', ('music')),
('travel', ('travel')),
('other', ('other')),
)
customer = models.OneToOneField(
'customers.CustomerProfile',
verbose_name=_('Customer'),
related_name='referencesprofile',
on_delete=models.CASCADE,
)
hobby = models.CharField(
('Hobby'),
max_length=250,
choices=HOBBY_CHOICES,
blank=True,
null=True,
)
what I want on my old data done automatically:
sport = ["skiing", "football"]
if hobby in sport :
hobby = "sport"
else :
hobby = "other"
return hobby
but I don't know in which function to write this code
Use a Data migration by running RunPython operation in the migration as stated in the documentation
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.