Hi Julio,

Thanks, now is clear.

Ok, I created models correctly and createa book in admin interface. Now I can create chapters to add this book.

Now I want to learn how to create an interface that allows an user to add a new book, click on next button and add chapters to this book that is selected by default in book select list.

Can you point me in a tutorial, or document?

Thanks in advance,

Germano


Il 09/08/2018 22:59, Julio Biason ha scritto:
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 <germano.care...@gmail.com <mailto:germano.care...@gmail.com>> 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 django-users+unsubscr...@googlegroups.com
    <mailto:django-users+unsubscr...@googlegroups.com>.
    To post to this group, send email to django-users@googlegroups.com
    <mailto:django-users@googlegroups.com>.
    Visit this group at https://groups.google.com/group/django-users
    <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
    <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 django-users+unsubscr...@googlegroups.com <mailto:django-users+unsubscr...@googlegroups.com>. To post to this group, send email to django-users@googlegroups.com <mailto:django-users@googlegroups.com>.
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 <https://groups.google.com/d/msgid/django-users/CAEM7gE0rAfJPYbgKxgHCRYwVuAEBGQrz5X2WxOJdOW6mG%3DzK3Q%40mail.gmail.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

--
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 django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
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/56a615c7-da95-c0b4-2079-a3b581e43c39%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to