class Relationship(models.Model):
from_user = models.ForeignKey(User, related_name='from_user')
to_user = models.ForeignKey(User, related_name='to_user')
status = models.CharField(max_length=255, choices=RELATIONSHIP_STATUSES)
field = models.ManyToManyField('User', through=Relationship, symmetrical=False, related_name='related_to')
field.contribute_to_class(User, 'relationships')
这工作正常关注和取消关注
users = User.objects.all()
{%for u in users %}
{% if not request.user in u.related_to.all %}
<input type="submit" value="Follow"/>
{% else %}
<input type="submit" value="UnFollow"/>
{% endif %}
{%endfor%}
但我想使用 User 对象获取关系状态字段,以找出所有用户的状态是关注或与经过身份验证的用户相互。我不想创建循环。就像上面的关注和取消关注一样,我在里面检查了 request.user ,就像我想检查状态是相互的还是关注的
users = User.objects.all()
{% for u in users %}
// here I want to get the Relationship status field using the User object to find out the status of all user is Following or mutual with authenticated user
{% endfor %}