What does pk__in mean in Django?

Model.objects.filter(pk__in=[list of ids])

and

Model.objects.filter(pk__in=[1,2,3])

How to show this data in a template?

def xx(request):
    return HttpResponse(Model.objects.filter(pk__in=[1,2,3]))
+3
source share
1 answer

This means that all objects in the model Modelare either 1, 2or 3both of the primary key.

See Search in the field - c .

You will get a list of objects back that you can show in the template, like any other list, using the tag for:

{% for object in objects %}
    Some value: {{ object.value }}
{% endfor %}

I do not want to offend you, but what is your problem? All this is well described in the documentation.

To learn how to create a Django application, you should read a tutorial or Django book .

+13
source

Source: https://habr.com/ru/post/1731349/


All Articles