繁体   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