Hi Germano,

When you use "ManyToMany", you're saying "any book can have any chapter and
vice versa". That's obviously what you don't want. What you want is "A book
has a closed set of chapters". This is done by thinking a bit on the other
way with Django: Instead of saying "There is a list of chapters of this
book", you say "The chapter belongs to a single book" with a foreign key:

class Book(models.Model):
   title = models.CharField(max_length=250)

class Chapters(models.Model):
   title = models.CharField(max_length=250)
   text = models.TextField()
   book = models.ForeignKey(Book)

Now a chapter can belong only to a book.

On Thu, Aug 9, 2018 at 4:34 PM, Germano Carella <[email protected]>
wrote:

> Hi,
> I'm Germano from Italy. I'm new of django.
> Probably this discussion has many many examples, but I can't find
> my situation.
> I have two models, Book and Chapters.
> These are simple models, I need them only for educational purpose.
>
> class Book(models.Model):
>     title=models.CharField(max_length=250)
>     chapters = models.ManyToManyField('Chapters')
>
> class Chapters(models.Model):
>     title_of_chapter = models.CharField(max_length=250)
>     text=models.TextField()
>
> Now, in admin interface I can add chapters on a new book and it works fine.
> So, whdn I try to add a second book I can see chapters of book I added
> previously.
>
> When I add a new book I want see only chapter of this book. List must be
> empty if I'm adding a new book.
> It's very simple, but I can't find a way to make it working.
>
> I tried somethings, such as
> chapters= models.ManyToManyField('Chapters',limit_choices_to=Q('
> book__title'=title))
> But this not works.
> I know Chapters has book_set, that is the set of references for a chapter.
>
> There is a way to make it working?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/2f104d64-50f4-433b-a764-be29d9a77f0c%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/2f104d64-50f4-433b-a764-be29d9a77f0c%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>



-- 
*Julio Biason*, Sofware Engineer
*AZION*  |  Deliver. Accelerate. Protect.
Office: +55 51 3083 8101 <callto:+555130838101>  |  Mobile: +55 51
<callto:+5551996209291>*99907 0554*

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAEM7gE0rAfJPYbgKxgHCRYwVuAEBGQrz5X2WxOJdOW6mG%3DzK3Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to