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

Reverse Relationships? #18

Open
neighlyd opened this issue May 12, 2018 · 5 comments
Open

Reverse Relationships? #18

neighlyd opened this issue May 12, 2018 · 5 comments

Comments

@neighlyd
Copy link

Hello,

I may be missing something, so I apologize if I am, but I am unable to get DRF-Flex-Fields to work properly with a reverse relationships . Is this not something that drf-flex-fields can do or am I messing up somehow?

@rsinger86
Copy link
Owner

Hi there, this should work with reverse relationships. You'd just need to make sure that the "source" arg matches what you've configured via the related= arg on your model FK.

Would help me debug if you could post some code of your model and serializer.

@neighlyd
Copy link
Author

Hi,

Thanks for the response. I apologize for not posting code before.

It seems that the issue may not actually be related to reverse relationships as I had previously thought. Following your response, I was able to get them working using the related= argument (or, alternatively, the <model>_set notation).

It appears that the real issue I was having was actually an order of operations one. Since my reverse relation serializer is defined below the serializer in which I want to expand the set, I can't reach it (either it is undefined at runtime, or if I use quotations as per normal DRF reference, it spits an error). An example may help.

serializers.py
class ArchiveSerializer(FlexFieldsModelSerializer):
    class Meta:
        model = models.Archive
        name = 'archive'
        fields = ('id', 'name', 'website', 'notes')

    expandable_fields = {
        'record_set': (RecordSerializer, {'source': 'record_set', 'many': True})
    }

class RecordSerializer(FlexFieldsModelSerializer):
    class Meta:
        model = models.Record
        fields = ('id', 'name', 'record_type', 'reel', 'notes', 'archive')

    expandable_fields = {
        'archive': (ArchiveSerializer, {'source': 'archive'})
    }

This throws an error when the server is run, because RecordSerializer is undefined.

If I put RecordSerializer in quotes within ArchiveSerializer like so:

class ArchiveSerializer(FlexFieldsModelSerializer):
    class Meta:
        model = models.Archive
        name = 'archive'
        fields = ('id', 'name', 'website', 'notes')

    expandable_fields = {
        'record_set': ('RecordSerializer', {'source': 'record_set', 'many': True})
    }

The error goes away and the server runs. However, when I navigate to ArchiveSerializer I receive the following error:

list index out of range

It looks like it's getting tripped up on line 77 of DRF-Flex-Field's serializers.py.

Obviously, having a circular expansion would be no good, but since children often expand their parents and parents often have need to expand a set of their children I'm finding the need to reference circularly (though not expanding circularly). Is the only alternative to have two versions of RecordSerializer?

@rsinger86
Copy link
Owner

rsinger86 commented May 20, 2018

Ah, so this is something I need to document. If you want to lazily evaluate the reference to your nested serializer class from a string inside expandable_feilds, you need to use this syntax:

expandable_fields = {
    'record_set': ('<app_name>.RecordSerializer', {'source': 'record_set', 'many': True})
}

Substitute the name of your Django app where the serializer is found for <app_name>. But if the serializer exists in the same app that it is called from, I really should allow the syntax that you used... Similar to how Django evaluates references to model FKs. I'll look to update this in a future version. Thanks for raising this issue.

@neighlyd
Copy link
Author

Oh great! Thanks so much for the information. I will go ahead and refactor my code to that instead of having a bunch of extra serializers.

I appreciate it!!

@willcroft
Copy link

+1 for this info in the docs! Ran into the same issue by wanting to keep my serializers in alphabetical order. Thanks so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants