[英]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.