Django, the relationship, the relationship between the three tables, the question of a beginner


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
52 views
Good day
gradually move from php to django.
After several consultations with psychologists, there are questions on the merits:

I want to implement my photo gallery with, well, you understand what
Have
Category
Gallery
Photos

I want to conclude:

The name of the category Photo gallery Photo Photo The name of the category Photo gallery Photo Photo The name of the category Photo gallery Photo Photo


What comes to mind:

def index(request): gallery = Gallery.objects.all().order_by('created') photos = {} for the gal in gallery: p = port.photos.all() if(len(p)): photos[gal.category] = [] photos[gal.category].append((gal, p))


and run through the array of photos, but as the template to make
for category in photos
for x,y in photos[category]
?

do you need to do
photos.append((gal.category gal, p))

??
by | 52 views

4 Answers

0 like 0 dislike
the template should look something like this
{% for gallery in galleries %}
gallery.photo_set.all
{% endfor %}
\r
not tried it, but it seems to work
by
0 like 0 dislike
Just select all the photos you want with select_related for galleries and categories, and in Sabine sgrupiruyte them using the tag regroup.
by
0 like 0 dislike
Try this
in view
galleries = Gallery.objects.all().order_by('created')
\r
in the template:
for gallery in galleries:
gallery.photo_set().all().order_by('created')
\r
Doc with examples here: docs.djangoproject.com/en/1.2/topics/db/queries/
by
0 like 0 dislike
The question you answered a couple of other things for the future:
\r
1. Use related_name. Then, instead of gallery.photo_set.all would be to write a gallery.photos.all (or any other name you like).
2. Set the sorting in the Meta.
3. There can never write a view and use direct_to_template.
4. If you want to do the same type of sample (like the last 10 records sorted by date), it is best to put these actions in separate Manager and call him. Encapsulation and all that. :-)
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
asked Apr 15, 2019 by Infinite
0 like 0 dislike
3 answers
0 like 0 dislike
2 answers
110,608 questions
257,186 answers
0 comments
28,043 users