The book
field in the model Chapter
can be deducted from the place
field, so you should discard it, then access to it by place.book
.
Only display ForeignKeys that pass a condition in Django.
-
19-10-2022 - |
Question
I have the following code:
from django.db import models
# Create your models here.
class Book(models.Model):
title = models.CharField(max_length=200)
is_finished = models.BooleanField("Is this book finished?")
description = models.TextField("What's your book about? Think of this as the back cover")
def __unicode__(self):
return self.title
#book_cover = should take an img as input
class Place(models.Model):
book = models.ForeignKey(Book)
name = models.CharField(max_length=200)
description = models.TextField()
def __unicode__(self):
return self.name
class Chapter(models.Model):
book = models.ForeignKey(Book)
name = models.CharField(max_length=200)
text = models.TextField()
place = models.ForeignKey(Place)
def __unicode__(self):
return self.name
The last class is giving my trouble. I want to be able to choose from a Place only if that Place is also in the same Book as my Chapter will be.
How can I accomplish this? I'm extremely new to Django and programming in general, sorry if my question is easily answered. I've tried searching through the Django docs but nothing seemed to work.
No correct solution
OTHER TIPS
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow