I am trying to build activity logs by a User or any other model object, to 
which a user can follow/subscribe.

These are the models related with logging the activities and following them:














*class Activity(models.Model):    """    An activity log : Actor acts on 
target    Actor can be a User or a model Object    """    
actor_content_type = models.ForeignKey(        ContentType, 
on_delete=models.CASCADE, related_name="actor_type")    actor_object_id = 
models.PositiveIntegerField()    actor = 
GenericForeignKey('actor_content_type', 'actor_object_id')    description = 
models.TextField()    target_content_type = models.ForeignKey(        
ContentType, on_delete=models.CASCADE, related_name="target_type")    
target_object_id = models.PositiveIntegerField()    target = 
GenericForeignKey('target_content_type', 'target_object_id')*
*    timestamp = models.DateTimeField(auto_now_add=True)*









*class Follow(models.Model):    """    A user can follow any User or model 
objects.    """    user = models.ForeignKey(User, 
on_delete=models.CASCADE)    content_type = models.ForeignKey(ContentType, 
on_delete=models.CASCADE)    object_id = models.PositiveIntegerField()    
follow_object = GenericForeignKey('content_type', 'object_id')*

Suppose:

   - Thor follows user Loki // User follows User
   - Thor follows user Stark // User follows User
   - Thor follows project Avenger // User follows Project

And suppose these are activities:

   - Nick Fury Created a project "Shield"
   - Nick Fury Created a project "Avengers"
   - Stark created a project "Mark II"
   - Avengers added Vision
   - Dr Strange created a project Dormammu

I can get the user *Thor* :
*thor = User.objects.get(email="t...@gmail.com")* 

And get the list of users/objects followed by *Thor*:

*thor.follow_set.all() *
*<QuerySet [<Follow: t...@gmail.com follows l...@gmail.com>, <Follow: 
t...@gmail.com follows st...@gmail.com>, <Follow: t...@gmail.com follows 
Avengers>]> *

Now to get the list of activities followed by the user Thor, I tried this:


* q = Q(actor__in=thor.follow_set.all())| Q(target__in= 
thor.follow_set.all()) *
*Activity.objects.filter(q) *

But its throwing an error:

*FieldError: Field 'actor' does not generate an automatic reverse relation 
and therefore cannot be used for reverse querying. If it is a 
GenericForeignKey, consider adding a GenericRelation.*

I can get all the activities for a single object followed by user Thor:

*followed_last = thor.follow_set.last().follow_object *


*q = Q(actor_content_type=ContentType.objects.get_for_model(followed_last), 
actor_object_id=followed_last.id)| 
Q(target_content_type=ContentType.objects.get_for_model(followed_last), 
target_object_id=followed_last.id)) *

* Activity.objects.filter(q)*
*<QuerySet [<Activity: Avengers Added vis...@gmail.com>, <Activity: 
nickf...@gmail.om Created Avengers>]> *

But how can I get all the activities Thor is following from the above 
activities:

   - Nick Fury Created a project "Avengers"
   - Stark created a project "Mark II"
   - Avengers added Vision

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/a2f82ae0-10c4-4a52-b960-90a6032b4799n%40googlegroups.com.

Reply via email to