簡體   English   中英

Django:當 model 成為選擇字段時更改數據

[英]Django : Change data when model become a choice field

我有一個表格,可以詢問人們的愛好。 這是我形式的Charfield。 我用多種選擇改變了這個領域。 對於我的舊數據,我想更新它們的值:例如,如果他們輸入:“football”、“skiing”,則必須更改為“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,
    )

我想要的舊數據自動完成:

sport = ["skiing", "football"]
if hobby in sport :
   hobby = "sport"
else :
   hobby = "other"
return hobby

但我不知道在哪個 function 寫這段代碼

文檔中所述,通過在遷移中運行 RunPython 操作來使用數據遷移

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM