如何从django中的另一个对象中删除一个对象?python -

时间:2022-09-25 07:49:42

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?

我有一个由Profile.objects.all()创建的queryset。我想在模板中打印它,除了它的一行。我如何在模板中做呢?或者如果在模板中不可能,我如何在视图中执行呢?

2 个解决方案

#1


0  

You could use .exclude() queryset like this :

您可以使用.exclude() queryset, like this:

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

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

这个Django查询将返回模型中的所有对象,而没有排除对象。

You have django documentation there : .exclude()

这里有django文档:.排除()

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)...']>

< / span > < span style = ' font - size: 9.0 pt; font - family:宋体;mso - ascii - font - family: calibri;(剩余元素截断)……”)>

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

Return :

返回:

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

, <: 3个个体测试>,'…(剩余元素截断)……”)>

#2


1  

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.

首先,Profile.objects.all()是一个QuerySet。您可以通过遍历QuerySet中每个实例的__str__()方法。

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 %}

#1


0  

You could use .exclude() queryset like this :

您可以使用.exclude() queryset, like this:

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

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

这个Django查询将返回模型中的所有对象,而没有排除对象。

You have django documentation there : .exclude()

这里有django文档:.排除()

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)...']>

< / span > < span style = ' font - size: 9.0 pt; font - family:宋体;mso - ascii - font - family: calibri;(剩余元素截断)……”)>

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

Return :

返回:

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

, <: 3个个体测试>,'…(剩余元素截断)……”)>

#2


1  

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.

首先,Profile.objects.all()是一个QuerySet。您可以通过遍历QuerySet中每个实例的__str__()方法。

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 %}