django - 按模型类的不同字段过滤

时间:2015-01-26 18:55:49

标签: django django-views

我有一个用户活动模型:

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 Meta:
        verbose_name = 'Activity'
        verbose_name_plural = 'Activities'
        ordering = ['-pub_date']

    def __unicode__(self):
        return ("%s %s") % (self.actor.username, self.action)

的观点:

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()
        users = []
        actors = list(following.values_list('user', flat=True)) + [user.id]
        activities = Activity.objects.filter(actor__in=actors)            

更新

假设3个用户喜欢相同的状态,而user3是最后一个喜欢该状态的用户。现在,这将创建3个活动,说“User1喜欢状态”,“User2喜欢状态”,“User3喜欢状态”。我想要的只是每个状态获得一个最近的活动。所以在这种情况下,它将是“User3 like status”。

状态模型:

class Status(models.Model):
    body = models.TextField(max_length=200)
    image = models.ImageField(blank=True, null=True, upload_to=get_upload_file_name)
    privacy = models.CharField(max_length=1,choices=PRIVACY, default='F')
    pub_date = models.DateTimeField(auto_now_add=True, auto_now=False)
    user = models.ForeignKey(User)
    hearts = generic.GenericRelation(Heart, null=True, blank=True)

class Heart(models.Model):
    user = models.ForeignKey(User)
    pub_date = models.DateTimeField(auto_now_add=True, auto_now=False)
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

更新

这是signals.py来创建每个对象的活动(心脏或状态):

def create_activity_item(sender, instance, signal, *args, **kwargs):
    if kwargs.get('created', True):
        ctype = ContentType.objects.get_for_model(instance)
        if ctype.name == 'Status':
            action = ' shared '

            activity = Activity.objects.get_or_create(
                actor = instance.user,
                action = action,
                content_type = ctype,
                object_id = instance.id,
                pub_date = instance.pub_date
            )

        if ctype.name == 'Heart':
            action = ' gave heart to '

            activity = Activity.objects.get_or_create(
                actor = instance.user,
                action = action,
                content_type = ctype,
                object_id = instance.id,
                pub_date = instance.pub_date
            )

for modelname in [Status, Heart]:
    post_save.connect(create_activity_item, sender=modelname,
                  dispatch_uid="create_activity_item")

3 个答案:

答案 0 :(得分:0)

activities = Activity.objects.filter(actor__in=users).distinct('content_type', 'object_id', 'action')

如果您的数据库不支持DISTINCT ON查询,您可以执行查询,然后手动运行查找不同值的对象:

unique_activities = []
activities = Activity.objects.filter(actor__in=users)
for activity in activities:
    unseen = True
    for unique_activity in unique_activities:
        if (activity.action == unique_activity.action and activity.content_type == unique_activity.content_type and activity.object_id == unique_activity.object_id):
            unseen = False
            break
    if unseen:
         unique_activities.append(activity)

print unique_activities

答案 1 :(得分:0)

这听起来像greatest-per-group问题。你能试试这个:

activities = Activity.objects.filter(
    id__in=Activity.objects.values('content_type') \
        .annotate(max_id=Max('id')) \
        .values_list('max_id', flat=True) \
)

答案 2 :(得分:0)

这只适用于PostgreSQL:

Activity.objects.order_by('action', 'content_type_id', 'object_id', '-pub_date').distinct('action', 'content_type_id', 'object_id').filter(actor__in=actors)