msln
msln

Reputation: 1523

How to remove an object from another object in django? - python

I have a queryset that created by Profile.objects.all(). I want to print it in template except just one of its rows. How can I do it in template? or if it's not possible in template, how can I do it in view?

Upvotes: 2

Views: 139

Answers (2)

Essex
Essex

Reputation: 6128

You could use .exclude() queryset like this :

YourObjet = Profile.objects.exclude(**kwargs)

This Django Query will return all objects in your Model without excluded objects.

You have django documentation there : .exclude()

Example :

MyObject = Individu.objects.all()

Return :

<QuerySet [<Individu: 1 19312STRASBOURG-402541 JUNGBLUTH Valentin>, <Individu: 2 18812STRASBOURG-797846 ARNOUD Laurent>, <Individu: 3 None TEST Test>, '...(remaining elements truncated)...']>

MyObject = Individu.objects.exclude(id="2")

Return :

<QuerySet [<Individu: 1 19312STRASBOURG-402541 JUNGBLUTH Valentin>, <Individu: 3 None TEST Test>, '...(remaining elements truncated)...']>

Upvotes: 0

zaidfazil
zaidfazil

Reputation: 9235

First of all Profile.objects.all() is a QuerySet. You can print out the __str__() method of each instance in the QuerySet just by iterating through it.

If you only want to neglect the last one, you could something like this,

{% for item in profiles %}

    {% if not forloop.last %}

        {{ item }}

    {% endif %}

{% endfor %}

Upvotes: 1

Related Questions