[Solved]-How to filter multiple fields with list of objects

3👍

What the docs are suggesting is not a bad thing.

The problem is that when you are creating Activities you are using auth.User as an actor, therefore you can’t add GenericRelation to auth.User (well maybe you can by monkey-patching it, but that’s not a good idea).

So what you can do?

@Sardorbek Imomaliev solution is very good, and you can make it even better if you put all this logic into a custom QuerySet class. (the idea is to achieve DRY-ness and reausability)

class ActivityQuerySet(models.QuerySet):
    def for_user(self, user):
        return self.filter(
            models.Q(
                actor_type=ContentType.objects.get_for_model(user),
                actor_id__in=user.relationship.follows_user.values_list('id', flat=True)
            )|models.Q(
                tags__in=user.relationship.follows_tag.all()
            )
        )

class Activity(models.Model):
    #..
    objects = ActivityQuerySet.as_manager()

#usage
user_feed = Activity.objects.for_user(request.user)

but is there anything else?

1. Do you really need GenericForeignKey for actor? I don’t know your business logic, so probably you do, but using just a regular FK for actor (just like for the tags) will make it possible to do staff like actor__in=users_following.

2. Did you check if there isn’t an app for that? One example for a package already solving your problem is django-activity-steam check on it.

3. IF you don’t use auth.User as an actor you can do exactly what the docs suggest -> adding a GenericRelation field. In fact, your Relationship class is suitable for this purpose, but I would really rename it to something like UserProfile or at least UserRelation. Consider we have renamed Relation to UserProfile and we create new Activities using userprofile instead. The idea is:

class UserProfile(models.Model):
    user = AutoOneToOneField('auth.user')
    follows_user = models.ManyToManyField('UserProfile', related_name='followed_by')
    follows_tag = models.ManyToManyField(Tag)

    activies_as_actor = GenericRelation('Activity',
        content_type_field='actor_type',
        object_id_field='actor_id',
        related_query_name='userprofile'
    )


class ActivityQuerySet(models.QuerySet):
    def for_userprofile(self, userprofile):
        return self.filter(
            models.Q(
                userprofile__in=userprofile.follows_user.all()
            )|models.Q(
                tags__in=userprofile.relationship.follows_tag.all()
            )
        )


class Activity(models.Model):
    #..
    objects = ActivityQuerySet.as_manager()

#usage

#1st when you create activity use UserProfile
Activity.objects.create(actor=request.user.userprofile, ...)

#2nd when you fetch. 
#Check how `for_userprofile` is implemented this time
Activity.objects.for_userprofile(request.user.userprofile)
👤Todor

6👍

You should just filter by ids.

First get ids of objects you want to filter on

following_user = userA.relationship.follows_user.all().values_list('id', flat=True)
following_tag = userA.relationship.follows_tag.all()

Also you will need to filter on actor_type. It can be done like this for example.

actor_type = ContentType.objects.get_for_model(userA.__class__)

Or as @Todor suggested in comments. Because get_for_model accepts both model class and model instance

actor_type = ContentType.objects.get_for_model(userA)

And than you can just filter like this.

Activity.objects.filter(Q(actor_id__in=following_user, actor_type=actor_type) | Q(tags__in=following_tag))

2👍

As stated in the documentation:

Due to the way GenericForeignKey is implemented, you cannot use such fields directly with filters (filter() and exclude(), for example) via the database API. Because a GenericForeignKey isn’t a normal field object, these examples will not work:

You could follow what the error message is telling you, I think you’ll have to add a GenericRelation relation to do that. I do not have experience doing that, and I’d have to study it but…

Personally I think this solution is too complex to what you’re trying to achieve. If only the user model can follow a tag or authors, why not include a ManyToManyField on it. It would be something like this:

class Person(models.Model):
    user = models.ForeignKey(User)
    follow_tag = models.ManyToManyField('Tag')
    follow_author = models.ManyToManyField('Author')

You could query all followed tag activities per Person like this:

Activity.objects.filter(tags__in=person.follow_tag.all())

And you could search ‘persons’ following a tag like this:

Person.objects.filter(follow_tag__in=[<tag_ids>])

The same would apply to authors and you could use querysets to do OR, AND, etc.. on your queries.

If you want more models to be able to follow a tag or author, say a System, maybe you could create a Following model that does the same thing Person is doing and then you could add a ForeignKey to Follow both in Person and System

Note that I’m using this Person to meet this recomendation.

1👍

You can query seperately for both usrs and tags and then combine them both to get what you are looking for. Please do something like below and let me know if this works..

usrs = Activity.objects.filter(actor__in=following_user)    
tags = Activity.objects.filter(tags__in=following_tag)

result = usrs | tags

1👍

You can use annotate to join the two primary keys as a single string then use that to filter your queryset.

from django.db.models import Value, TextField
from django.db.models.functions import Concat

following_actor = [
    # actor_type, actor
    (1, 100),
    (2, 102),
]
searchable_keys = [str(at) + "__" + str(actor) for at, actor in following_actor]

result = MultiKey.objects.annotate(key=Concat('actor_type', Value('__'), 'actor_id', 
                                              output_field=TextField()))\
    .filter(Q(key__in=searchable_keys) | Q(tags__in=following_tag))
👤Du D.

Leave a comment