문제

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.

올바른 솔루션이 없습니다

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top