Trouble with QuerySets

56 views
Skip to first unread message

delemos...@gmail.com

unread,
Jun 25, 2016, 4:11:56 PM6/25/16
to django-reversion discussion group
Hey I am trying to render a view for all of the Version instances of a particular object. I am using the code below to add a QS into Context: 
context['history'] = Version.objects.get_for_object(self.get_object())
When I do this it renders a 'VersionQuerySet' for the object but I haven't been able to find a way to iterate through it and retrieve the information I need. When I do this through the Python console it can iterate and pull information from the field_dict[]. 

I have also tried:
context['history'] = VersionQuerySet.get_for_object(obj=self.object, model_db=None) 
Every time I use this I get an error: 
TypeError: get_for_object() missing 1 required positional argument: 'self'


Can anyone suggest on how to do this or what I am doing wrong? Thanks! 

Dave Hall

unread,
Jun 27, 2016, 4:52:30 AM6/27/16
to django-reversion discussion group
In your templates you can do something like:

{% for version in history %}
    <p>{{version.field_dict.some_field}}</p>
{% endfor %}

The second error is because get_for_objectI() is an instance method, not a class method. I've updated the docs to clarity. Version.objects.get_for_object(obj) is the correct usage.


--
You received this message because you are subscribed to the Google Groups "django-reversion discussion group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-reversi...@googlegroups.com.
To post to this group, send email to django-r...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-reversion.
For more options, visit https://groups.google.com/d/optout.

delemos...@gmail.com

unread,
Jun 27, 2016, 7:26:27 PM6/27/16
to django-reversion discussion group
Thanks Dave. I actually ended up creating a template_tag that would do that for me so i didn't need the loop each time. 

Another question: Is there a way to access the serialized data of a FK that has been followed and stored. I know that information is there because I can see it in the admin view but I am trying to access it in a regular view. 

Thanks! 

Dave Hall

unread,
Jun 28, 2016, 4:17:46 AM6/28/16
to django-reversion discussion group
Sure there is! It's a bit complicated, and feels like a shortcut helper would be nice, but here's how you do it:

version = Version.objects.get_for_object(your_obj)
fk_id = version.field_dict["your_fk_id")
revision = version.revision
related_version = revision.version_set.get(content_type=ContentType.objects.get_for_model(YourRelated), object_id=fk_id)
Reply all
Reply to author
Forward
0 new messages