GeeWee / django-auto-prefetching

Automatic prefetching for Django
MIT License
231 stars 18 forks source link

dotted notation to traversel in serializer gives AttributeError #37

Closed Javedgouri closed 2 years ago

Javedgouri commented 3 years ago

dotted notation to traverse attributes are not working

user_platforms = UserPlatformSerializer(source='user.user_platforms', read_only=True, many=True)

AttributeError: Cannot find 'user.user_platforms' on Artist object, 'user.user_platformsplatformlogo' is an invalid parameter to prefetch_related()

GeeWee commented 3 years ago

I'd love for this to get fixed - unfortunately I don't have a lot of time to dedicate to this project currently. Pull requests are very welcome.

sgarcialaguna-mms commented 2 years ago

I recently stumbled over this. Couldn't one simply do

    select_related = [s.replace('.', '__') for s in select_related]
    prefetch_related = [s.replace('.', '__') for s in prefetch_related]

I tried this out locally and it seemed to work, but I'm not sure it won't break in other places.

GeeWee commented 2 years ago

Yeah that could probably work - we have a reasonably extensive test suite so if you want to submit a PR we should be able to see if it actually breaks anything :)

sgarcialaguna-mms commented 2 years ago

Sure, why not:

https://github.com/GeeWee/django-auto-prefetching/pull/44

GeeWee commented 2 years ago

Awesome.

GeeWee commented 2 years ago

0.2.11 on PyPi should have this change.