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

0 like 0 dislike
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

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 = 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 | 61 views

4 Answers

0 like 0 dislike
the template should look something like this
{% for gallery in galleries %}
{% endfor %}
not tried it, but it seems to work
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.
0 like 0 dislike
Try this
in view
galleries = Gallery.objects.all().order_by('created')
in the template:
for gallery in galleries:
Doc with examples here:
0 like 0 dislike
The question you answered a couple of other things for the future:
1. Use related_name. Then, instead of gallery.photo_set.all would be to write a (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. :-)

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
asked Apr 14, 2019 by Infinite
0 like 0 dislike
3 answers
0 like 0 dislike
2 answers
110,608 questions
257,186 answers
32,742 users