朋友们,我正在使用django-friendship与django 1.9来实现用户跟随。但是在“要关注的用户”页面上,我想要过滤那些没有被当前用户关注的用户。我该如何实现呢?
这是我的views.py
def users_to_follow(request):
follow = Follow.objects.following(request.user)
all_users = User.objects.all().exclude(follow)
return render(request,"auths/users_to_follow.html",locals())
这是我的users_to_follow.html
{% for u in all_users %}
{{ u.username }}
<a href="{% url 'follow' u.username %}">Follow</a>
{% endfor %}
我认为views.py有问题。但我无法弄明白。帮帮我朋友。
答案 0 :(得分:1)
follow
名称(变量)是用户对象的列表。您可以获取这些用户的ID:
follow = Follow.objects.following(request.user)
follow_ids = [x.id for x in follow]
然后像这样使用exclude
:
to_follow = User.objects.all().exclude(id__in=follow)
to_follow
列表应包含您想要的用户。
答案 1 :(得分:0)
class Program
{
static void Main(string[] args)
{
var now = DateTime.Now;
var ff = now.ToEorzeaTime();
Console.WriteLine($"Now: {now} | FF: {ff}");
var ffNew = new DateTime(ff.Ticks, DateTimeKind.Utc);
var nowNew = ffNew.ToEarthTime();
Console.WriteLine($"Now: {nowNew} | FF: {ffNew}");
Console.ReadLine();
}
}
public static class Converter
{
private const double EORZEA_MULTIPLIER = 3600D / 175D;
public static DateTime ToEorzeaTime(this DateTime date)
{
long epochTicks = date.ToUniversalTime().Ticks - (new DateTime(1970, 1, 1).Ticks);
long eorzeaTicks = (long)Math.Round(epochTicks * EORZEA_MULTIPLIER);
return new DateTime(eorzeaTicks);
}
public static DateTime ToEarthTime(this DateTime date)
{
var epochTicks = (long) Math.Round(date.Ticks/EORZEA_MULTIPLIER);
var earthTicks = epochTicks + new DateTime(1970, 1, 1).Ticks;
var utc = new DateTime(earthTicks, DateTimeKind.Utc);
return utc.ToLocalTime();
}
}
无法正常工作。您可以尝试以下方法:
exclude
答案 2 :(得分:0)
根据Django QuerySet documentation(强调我的):
字段查找是指定SQL WHERE子句的内容的方式。它们被指定为QuerySet方法filter(),exclude()和get()的关键字参数。
因此,您的exclude()
方法调用可以调整为
all_users = User.objects.all().exclude(pk=follow)
那就是它!