Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filtering on action_object: Field 'action_object' does not generate an automatic reverse relation and therefore cannot be used for reverse querying. #281

Closed
graup opened this issue Feb 26, 2016 · 5 comments
Labels
weirdness strange project configs and bugs

Comments

@graup
Copy link

graup commented Feb 26, 2016

Hi! I used your example from django-notifications/django-notifications#64 (comment) to add a Notification model that is saved as the action_object.

However, when in the stream I try to return sender.actor_actions.filter('action_object__read': False) (sender ist the current user), Django fails with Field 'action_object' 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 don't really understand this error, because I am not actually querying in reverse here, am I?

Any ideas?

@justquick
Copy link
Owner

please post the python code you are using and the full traceback of the error

@graup
Copy link
Author

graup commented Mar 5, 2016

I succeeded a bit with adding the GenericRelation and querying based on the related_name I gave that. However, then it failed because my model's pk was an Integer, while action_object_id is a CharField (which PostgreSQL doesnt like).

I'll create a test project to showcase this problem.

@graup
Copy link
Author

graup commented Mar 5, 2016

Here you are: https://github.com/graup/django-activity-stream-test

This also happens to reproduce #278.

@justquick
Copy link
Owner

@graup very helpful test project! been playing with it and in doing so i noticed a bug that is related to some other issues. unfortunately fixing it did not resolve this issue.

This is the approach that makes the most sense to me but is still far away from working:

# Version 1: Fails because of missing reverse relation
filters = {'action_object__read': False, 'action_object__deleted': False}

This is the solution that makes the least sense to me but is the closest to working:

# Version 2: Fails because of not matching db column types
filters = {'notification__read': False, 'notification__deleted': False}

This is the query that is being performed in version 2:

SELECT "actstream_action"."id", 
       "actstream_action"."actor_content_type_id", 
       "actstream_action"."actor_object_id", 
       "actstream_action"."verb", 
       "actstream_action"."description", 
       "actstream_action"."target_content_type_id", 
       "actstream_action"."target_object_id", 
       "actstream_action"."action_object_content_type_id", 
       "actstream_action"."action_object_object_id", 
       "actstream_action"."timestamp", 
       "actstream_action"."public" 
FROM   "actstream_action" 
       INNER JOIN "example_notification" 
               ON ( "actstream_action"."action_object_object_id" = 
                    "example_notification"."id" 
                    AND ( "actstream_action"."action_object_content_type_id" = 9 
                        ) ) 
WHERE  ( "actstream_action"."actor_object_id" = 1 
         AND "actstream_action"."actor_content_type_id" = 4 
         AND "example_notification"."deleted" = false 
         AND "example_notification"."read" = false ) 
ORDER  BY "actstream_action"."timestamp" DESC 

Ive been trying to get a CAST stmt setup on the INNER JOIN to make the types comparable. In django you can use the extra() queryset operator but I cant get it working on a gfk lookup:

http://stackoverflow.com/questions/28101580/how-do-i-cast-char-to-integer-while-querying-in-django-orm

Unfortunately these joins on generic relations can get a bit messy. Im going to play around with it some more and hopefully get the approach that makes the most sense working in the best way.

@maguri
Copy link

maguri commented Dec 29, 2016

Similar issue here.
Any idea?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
weirdness strange project configs and bugs
Projects
None yet
Development

No branches or pull requests

3 participants