需要访问django模板中的manytomanyfield

时间:2021-04-13 22:52:52

So i have these models:

所以我有这些模型:

class CofifiUser(models.Model):
  user = models.OneToOneField(User)

class QuoteIdea(models.Model):
  creator = models.ForeignKey(CofifiUser,related_name="creator")
  text = models.TextField(max_length=250)
  votes = models.CharField(max_length=100,default="0")
  votes_received = models.ManyToManyField(CofifiUser)
  created_at = models.DateTimeField(auto_now_add=True)

And i want:

而且我要:

If request.user.username is in the item.votes_received.all 
  <button class="disabled">Button</button>
Else 
  <button class="btn btn-primary">Button</button>

Is almost the same thing as the Like Button on Facebook . You cannot give more than one like to a page ( in python/django ) Please need some help here :)

几乎与Facebook上的Like Button相同。你不能给一个页面多一个(在python / django中)请在这里需要一些帮助:)

2 个解决方案

#1


1  

Something that's important to keep in mind here is the performance implication of having to query a many-to-many relationship for each comparison, or even pre-loading all of the m2m data to see if the user is in the votes_received queryset.

这里需要记住的重要一点是,必须为每次比较查询多对多关系,甚至预先加载所有m2m数据以查看用户是否在vote_received查询集中。

In a case like this, I usually opt for a de-normalized way to do the boolean comparison. I will create a field to just hold the IDs as comma-separated ints and update the field via a post_save signal.

在这种情况下,我通常选择去规范化的方式来进行布尔比较。我将创建一个字段,将ID保存为以逗号分隔的整数,并通过post_save信号更新字段。

This greatly simplifies things at the view and template level and also avoids having to do any joins, or any additional queries at all.

这极大地简化了视图和模板级别的操作,并且还避免了必须进行任何连接或任何其他查询。

Assuming you're looping over a list of QuoteIdea instances and passing an instance of CofifiUser to the template, and you have added a field to QuoteIdea called something like cofifi_vote_ids you can do:

假设您循环遍历QuoteIdea实例列表并将CofifiUser实例传递给模板,并且您已向QuoteIdea添加了一个名为cofifi_vote_ids的字段,您可以执行以下操作:

{# returns added to prevent wrapping #}

{% for quote_idea in quote_ideas %}
    <button class="btn 
        {% if cofifi_user.id in quote_idea.cofifi_vote_ids %}
        disabled
        {% else %}
        btn-primary
        {% endif %}">Button</button>
{% endfor %}

#2


0  

I did something like this and it worked very good.

我做了这样的事情并且工作得非常好。

<button class="btn btn-primary btn-xs 
{% for creator in item.votes_received.all %}
  {%if user.id == creator.id %}
    disabled
  {%endif%}
{% endfor %}
">Send Coffee</button>

#1


1  

Something that's important to keep in mind here is the performance implication of having to query a many-to-many relationship for each comparison, or even pre-loading all of the m2m data to see if the user is in the votes_received queryset.

这里需要记住的重要一点是,必须为每次比较查询多对多关系,甚至预先加载所有m2m数据以查看用户是否在vote_received查询集中。

In a case like this, I usually opt for a de-normalized way to do the boolean comparison. I will create a field to just hold the IDs as comma-separated ints and update the field via a post_save signal.

在这种情况下,我通常选择去规范化的方式来进行布尔比较。我将创建一个字段,将ID保存为以逗号分隔的整数,并通过post_save信号更新字段。

This greatly simplifies things at the view and template level and also avoids having to do any joins, or any additional queries at all.

这极大地简化了视图和模板级别的操作,并且还避免了必须进行任何连接或任何其他查询。

Assuming you're looping over a list of QuoteIdea instances and passing an instance of CofifiUser to the template, and you have added a field to QuoteIdea called something like cofifi_vote_ids you can do:

假设您循环遍历QuoteIdea实例列表并将CofifiUser实例传递给模板,并且您已向QuoteIdea添加了一个名为cofifi_vote_ids的字段,您可以执行以下操作:

{# returns added to prevent wrapping #}

{% for quote_idea in quote_ideas %}
    <button class="btn 
        {% if cofifi_user.id in quote_idea.cofifi_vote_ids %}
        disabled
        {% else %}
        btn-primary
        {% endif %}">Button</button>
{% endfor %}

#2


0  

I did something like this and it worked very good.

我做了这样的事情并且工作得非常好。

<button class="btn btn-primary btn-xs 
{% for creator in item.votes_received.all %}
  {%if user.id == creator.id %}
    disabled
  {%endif%}
{% endfor %}
">Send Coffee</button>