Skip to content

accessing many-to-many queryset of a version... #153

@jedie

Description

@jedie

One questions, how can i access a many-to-many field queryset of a version?

This doesn't work, because it's not the data from the version, it's the current obj data:

obj = version.object_version.object
related = getattr(obj, field.name)
queryset = related.all()

the same with this:

concrete_model = obj._meta.concrete_model
many_to_many_fields = concrete_model._meta.many_to_many
for field in many_to_many_fields:
    queryset = field.value_from_object(obj)

One way i get the right data is this:

obj = version.object_version.object
related = getattr(obj, field.name)
ids = version.field_dict[field.name]
queryset = many_related_manager.filter(pk__in=ids)

Is this a best practise solution?

EDIT: Find out that the last example doesn't work, too: many_related_manager.all() will not return all existing values...

It's for: jedie/django-reversion-compare@90b9c48#L0R70

EDIT2:
Some info of the objects in the example:
version: instance of reversion.models.Version
field: instance of django.db.models.fields.related.ManyToManyField and comes from: obj._meta.concrete_model._meta.many_to_many
many_related_manager: instance of django.db.models.fields.related.ManyRelatedManager

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions