我正在制作类似克隆的Twitter(只是为了学习Django的工作原理) 所以我基本上是想建立一个many_to_many关系。 我想向用户个人资料添加显示“ FOLLOWED_BY”和“ FOLLOWING”的功能,但页面上未显示“ FOLLOWED_BY”的列表,请有人帮我!
在models.py中,我定义了两个关系
user = models.OneToOneField(settings.AUTH_USER_MODEL,
on_delete=models.SET_NULL, related_name='profile', null=True,
blank=True)
following = models.ManyToManyField(settings.AUTH_USER_MODEL,
related_name='followed_by', blank=True)
和在user_detail.html中,我有关于配置文件外观的代码
这是models.py模块:
from django.conf import settings
from django.db import models
# Create your models here.
class UserProfile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL,
on_delete=models.SET_NULL, related_name='profile',
null=True,
blank=True)
following = models.ManyToManyField(settings.AUTH_USER_MODEL,
related_name='followed_by', blank=True)
def __str__(self):
return str(self.following.all().count())
下面是user_detail.html文件的代码:
{% extends "base.html" %}
{% block content %}
<div class="row">
<div class="col-sm-3 col-xs-12" style="background-color: yellow">
<h1>{{ object.username }}</h1>
<p>Followers: {{ object.followed_by.count }}</p>
</div>
<div class="col-sm-9 col-xs-12">
<h1>Tweets</h1>
{% for tweet in object.tweet_set.all %}
{{ tweet }}<br/>
{% endfor %}
<hr/>
<h1>Following</h1>
{% for user in object.profile.following.all %}
<a href='/{{ user.username }}'>{{ user.username }}</a><br/>
{% empty %}
<h4>Not following any users</h4>
{% endfor %}
<hr/>
<h1>Followed By</h1>
{% for profile in object.profile.followed_by.all %}
<a href='/{{ profile.user.username }}'>{{ profile.user.username }}</a><br/>
{% empty %}
<h4>Not followed by any user</h4>
{% endfor %}
</div>
{% endblock content %}
对于用户个人资料,我可以根据需要获取FOLLOWING字段,但FOLLOWED_BY字段未显示我该怎么做(我应该在代码中进行哪些更改)?
答案 0 :(得分:0)
您定义了一个def render_partial_resource(resources)
if resources.present?
if resources.respond_to?(:model_name)
folder = resources.model_name.name.pluralize.downcase
partial = resources.model_name.name.downcase
else
folder = resources.object.name.pluralize.downcase
partial = resources.object.name.downcase
end
resources.map do |resource|
render partial:"#{folder}/#{partial}", locals:{"#{partial}": resource}
end.join(" ").html_safe
end
end
字段,该字段指向 user 模型,而不是一个following
。结果,Profile
没有Profile
关系,而followed_by
对象却没有。
我认为让User
指向following
可能更好,例如:
Profile
然后您可以将其渲染为:
class UserProfile(models.Model):
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
on_delete=models.SET_NULL,
related_name='profile',
null=True,
blank=True
)
following = models.ManyToManyField(
'self',
related_name='followed_by',
symmetrical=False,
blank=True
)
def __str__(self):
return str(self.following.all().count())
但是,您的代码具有一些(严重)反模式。最重要的是,您应该不要在模板中编写业务逻辑。您应该为此使用视图。例如,您可以在视图中指定类似这样的上下文:
<div class="col-sm-3 col-xs-12" style="background-color: yellow">
<h1>{{ object.username }}</h1>
<p>Followers: {{ object.followed_by.count }}</p>
</div>
<div class="col-sm-9 col-xs-12">
<h1>Tweets</h1>
{% for tweet in object.tweet_set.all %}
{{ tweet }}<br/>
{% endfor %}
<hr/>
<h1>Following</h1>
{% for profile in object.profile.following.all %}
<a href='/{{ profile.user.username }}'>{{ profile.user.username }}</a><br/>
{% empty %}
<h4>Not following any users</h4>
{% endfor %}
<hr/>
<h1>Followed By</h1>
{% for profile in object.profile.followed_by.all %}
<a href='/{{ profile.user.username }}'>{{ profile.user.username }}</a><br/>
{% empty %}
<h4>Not followed by any user</h4>
{% endfor %}
</div>
我们现在还可以使用.select_related()
[Django-doc]来显着提高性能,因为现在所有用户都在同一查询中获取。
您最好也使用{% url ... %}
template tag [Django-doc]来构造查询。所以不用写:
context = {
'tweets': object.tweet_set.all()
'followers': object.profile.following.select_related('user').all()
'followed_by': object.profile.followed_by.select_related('user').all()
}
最好使用反向查找来构造查询,例如:
<a href="/{{ profile.user.username }}">