-
Notifications
You must be signed in to change notification settings - Fork 61
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
Comments
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 Would help me debug if you could post some code of your model and serializer. |
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 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 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
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 |
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
Substitute the name of your Django app where the serializer is found for |
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!! |
+1 for this info in the docs! Ran into the same issue by wanting to keep my serializers in alphabetical order. Thanks so much! |
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?
The text was updated successfully, but these errors were encountered: