New member: Germano Carella. I need help with django, limit_choices_to and admin interface

10 views
Skip to first unread message

Germano Carella

unread,
Aug 9, 2018, 4:54:54 PM8/9/18
to Django users
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? 

Julio Biason

unread,
Aug 9, 2018, 5:00:04 PM8/9/18
to django...@googlegroups.com
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.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django...@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/2f104d64-50f4-433b-a764-be29d9a77f0c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Julio Biason, Sofware Engineer
AZION  |  Deliver. Accelerate. Protect.
Office: +55 51 3083 8101  |  Mobile: +55 51 99907 0554

germano carella

unread,
Aug 9, 2018, 5:50:32 PM8/9/18
to django...@googlegroups.com

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

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

germano carella

unread,
Aug 10, 2018, 2:55:25 AM8/10/18
to django...@googlegroups.com

I found a tutorial!

I studied ForeignKey and how it works with sets... Now, I'll learn how to create a form and how to save the new data in set.

Thanks!



Il 09/08/2018 22:59, Julio Biason ha scritto:
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages