Multiple Image upload like ecommerce application

Oh, yes I just copied the last thanks

I have just two last question. This is my view to show all projects. But now I have two seperated models so projects = Project.objects.all() is probably not right now.

def projects(request):
    projects = Project.objects.all()
    context = {"projects":projects}
    return render(request, 'projects/projects.html', context)

And how can I adresse all the images of one project in the template. I thought about this.
projects.html

    {% for project in projects %}
      {% for img in project.image_set.all %}
        <img class="project__thumbnail" src="{{img.image.url}}" alt="project thumbnail" />
      {% endfor %}
      <h3 class="project__title">{{project.title}}</h3>
      <h3 class="project__title">{{project.describtion}}</h3>

    {% endfor %}

Thanks

If you have an object of type Project, how do you access the set of ProjectImage objects that relate to that Project?
(See Making queries | Django documentation | Django if you need a hint.)

I thought it would be through _set.all. So in my case for img in project.image_set.all.
It´s also mentioned in the documentation but it seems to be wrong

Right basic idea, but re-read that first paragraph of those docs again.

As far as I understood models with ForeignKey like my model ProjectImage have acess to a Manager which returns QuerySets. These can be filtered with functions like get() or in my case with _set.all() right? So I thought it should be right

Close, not quite. Yes, a manager returns querysets. In a query, something like Projects.objects.all(), objects is the name of the manager.

In a foreign key relationship, Django creates a manager for you. That manager (by default) ends with _set. In other words, _set is part of the manager name - not part of the filter.

Very specifically from the documentation - the documentation says the related object manager (by default) is FOO_set. What is FOO supposed to be?

1 Like

FOO seems to be the source model name

So if you have an object of type Project, lets call it project, how would you access all the ProjectImage objects related to it?

Is it project.projectImage.set_all()

Specifically, what does the referenced docs say, in the second sentence, is the name of the manager?

By default, this manager is named FOO_set

, where FOO is … (what?)

And so, what would your reference then look like?

where FOO is the source model name
So in my case the source model name is project

No, in this case, the “source model” is the model with the ForeignKey field. In a ForeignKey relationship, the model with the ForeignKey is the “source”, and the model being referenced by the ForeignKey is the “related” or “target” model.

Ok so it must be the ProjectImage model
So the template should look like this?

    {% for project in projects %}
        {% for img in projectimage.image_set.all %}
          <img class="project__thumbnail" src="{{img.image.url}}" alt="project thumbnail" />
        {% endfor %}
    {% endfor %}

Let’s take a step back here.

If I have the expression:
Project.objects.all()

What part of this expression is the Manager?
(See Managers | Django documentation | Django if needed. Also possibly Making queries | Django documentation | Django)

This retunrs a QuerySet which contains all the objects of Project

Correct.

What part of that statement is the Manager? (Or perhaps more precisely, is a reference to a manager.)

The manager is named FOO_set by default. So in my case Project_set