dabapps / django-rest-framework-serialization-spec

DEPRECATED, see https://github.com/dabapps/django-readers instead
MIT License
11 stars 0 forks source link

For a reverse many-to-many relation we should not (cannot) prefetch the reverse FK #51

Closed pmg103 closed 4 years ago

pmg103 commented 4 years ago

Bug discovered by @CharlieHarle which prevented fetching reverse many-to-many relations.

Eg this did not work:

class User(Model):
    id = UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

class UserGroup(Model):
    users = ManyToManyField(User, blank=True)

class UsersList(SerializationSpecMixin, ListAPIView):
    queryset = User.objects.all()
    serialization_spec = [
        {'usergroup_set': [
            'id',
            'name'
        ]}
    ]

because it would try to prefetch users_id on the related UserGroup which isn't possible and fails with:

django.core.exceptions.FieldDoesNotExist: UserGroup has no field named 'users_id'