JPG
JPG

Reputation: 88459

Django model choice field from another model instance

In my models.py, I have two classes, ChoiceList and SampleModel as below

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)

I need to add your_choice field data only from ChoiceList instances. Can I add data in that way ?

When I doing this way, I got error as django.db.utils.OperationalError: no such table: rest_api_ChoiceList
Can anyone solve the problem ?

Upvotes: 10

Views: 15954

Answers (1)

badiya
badiya

Reputation: 2257

you should use ForeignKey()

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

Upvotes: 22

Related Questions