[英]Django model choice field from another model instance
在我的models.py中 ,我有兩個類, ChoiceList
和SampleModel
,如下所示
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.