django - 通过过滤对象列表来获取对象列表 [英] django - get list of objects by filtering a list of objects

查看:100
本文介绍了django - 通过过滤对象列表来获取对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个用户活动流。

I am creating a user activity streams.

活动模式:

class Activity(models.Model):
    actor = models.ForeignKey(User)
    action = models.CharField(max_length=100)
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')
    pub_date = models.DateTimeField(auto_now_add=True, auto_now=False)

关系模型:

class Person(models.Model):
    user = models.OneToOneField(User)
    relationships = models.ManyToManyField('self', through='Relationship', 
                                           symmetrical=False, 
                                           related_name='related_to')
RELATIONSHIP_FOLLOWING = 1
RELATIONSHIP_BLOCKED = 2
RELATIONSHIP_STATUSES = (
    (RELATIONSHIP_FOLLOWING, 'Following'),
    (RELATIONSHIP_BLOCKED, 'Blocked'),
)

class Relationship(models.Model):
    from_person = models.ForeignKey(Person, related_name='from_people')
    to_person = models.ForeignKey(Person, related_name='to_people')
    status = models.IntegerField(choices=RELATIONSHIP_STATUSES)  

def get_relationships(self, status):
    return self.relationships.filter(
        to_people__status=status, 
        to_people__from_person=self)

def get_following(self):
    return self.get_relationships(RELATIONSHIP_FOLLOWING)

在视图中:

def home(request):
    if request.user.is_authenticated():
        user = request.user
        userP = Person.objects.get_or_create(user=user)
        userP = userP[0]
        following = userP.get_following()
        activities = Activity.objects.filter(actor__in=following)
        if request.POST:
            form = StatusForm(request.POST, request.FILES)
            if form.is_valid():
                if form.cleaned_data:
                    status = form.save(commit=False)
                    status.user = user
                    status.save()
                    return HttpResponseRedirect('/')

场景


  • 用户正在关注user2和user3

  • user2正在关注用户和user3

  • user3正在关注用户和user2

由于我使用 actor = user 而不是 actor = userP (这是类型为 user.person 的对象)。如何从列表 get_following()方法中获取用户对象的列表,而不是 user.person对象

Since I am filtering activities with actor=user and not actor=userP (which is an object of type user.person). How will I get a list of user object from the list get_following() method and not of user.person object.

然而,我可以循环遍历以下每个 如下所示:

I can however loop through each of the following like so:

    following = userP.get_following()
    users = []
    for u in following:
        users.append(u.user)
    users.append(user)
    activities = Activity.objects.filter(actor__in=users)

但我希望会有另一个更好的方法吗?

But I am hoping that there will be another better way to do it?

请指导我。您的帮助将非常感谢。谢谢。

Please guide me. Your help will be very much appreciated. Thank you.

推荐答案

使用queryset的 values_list()方法:

Use the queryset's values_list() method:

activities = Activity.objects.filter(actor__in=
                                     following.values_list('user', flat=True))

如果要将其他用户添加到演员列表中,则必须将 valies_list 从查询器转换为常规python列表:

If you want to add another user to actors list then you have to convert valies_list from queryset to regular python list:

actors = list(following.values_list('user', flat=True)) + [user.id]
activities = Activity.objects.filter(actor__in=actors)

这篇关于django - 通过过滤对象列表来获取对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆