簡體   English   中英

來自另一個模型實例的Django模型選擇字段

[英]Django model choice field from another model instance

在我的models.py中 ,我有兩個類, ChoiceListSampleModel ,如下所示

class ChoiceList(models.Model):
    choice=models.CharField(max_length=15)

class SampleModel(models.Model):
    CHOICELIST=ChoiceList.objects.all()
    name=models.CharField(max_length=15)
    your_choice=models.CharField(max_length=15,choices=ChoiceList)

我需要添加your_choice只來自野外數據的ChoiceList實例。 我可以這樣添加數據嗎?

當我這樣做時,我收到錯誤,因為django.db.utils.OperationalError: no such table: rest_api_ChoiceList
有人能解決這個問題嗎?

你應該使用ForeignKey()

your_choice=models.ForeignKey(ChoiceList,on_delete=models.CASCADE)

暫無
暫無

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

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