I creating an app where users can post with its related tags:
class Tag(models.Model):
name = models.CharField(max_length=255, unique=True)
class Post(models.Model):
user = models.ForeignKey(User)
body = models.TextField()
tags = models.ManyToManyField(Tag)
pub_date = models.DateTimeField(default=timezone.now)
activity = GenericRelation(Activity)
class Activity(models.Model):
actor = models.ForeignKey(User)
verb = models.PositiveIntegerField(choices=VERB_TYPE)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
pub_date = models.DateTimeField(default=timezone.now)
What I want to do is filter the Activity with the a list of users, and a list of tags of the from list of Post objects.
Eg:
UserA created a new Post obect with tags(#class, #school)
UserB created a new Post object with tags(#professor, #teacher)
UserC created a new Post object with tags(#school, #university)
UserD created a new Post object with tags(#university, #school)
So say I want to filter Activity with user_list=[UserA, UserC] and tag_list = [#class, #teacher]
It should return:
UserA created a new Post obect with tags(#class, #school)
UserC created a new Post object with tags(#school, #university)
UserB created a new Post object with tags(#professor, #teacher)
To filter the Activity with users, I can query this way:
Activity.objects.filter(actor__in=user_list)
But, how do I filter Activity with the content_object's (
i.e.Post) field (i.e. Post.tags)? Is this even possible? I cannot figure it out. And your help will be very much helpful. Thank you.