> <mailto:django-users+unsub...@googlegroups.com>.
On Wednesday 22 February 2017 00:39:42 Ankush Thakur wrote:
> I'm using DRF for an API. My problem is that I defined my postal
> address like this (breaking it up into City, State, Country):
...
> So there's a hell lot of nesting from city to state to country. If the
> relationship chain was even deeper, there would be even more nesting,
> which I feel isn't great for API consumers.
class AddressSerializer(serializers.ModelSerializer):
depth = 3
class Meta:
model = Address
fields = ['id', 'building_no', 'street', 'locality',
'landmark', 'pincode', 'latitude', 'longitude', 'city']
class DisplayAddressSerializer(AddressSerializer):
depth = 1
Now you can add display_address to any serializer using an address and API consumers can use it to render easily. You can then use AddressSerializer to provide the full structured address, so it can be used for writes.
You may need to customize how it flattens the models, but that's what DRF's documentation is for.
--
Melvyn Sopacua
If the relationship chain was even deeper, there would be even more nesting, which I feel isn't great for API consumers. What is the best practice here to put state and country at the same level as the city?
--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/ttoJbZJOBnU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a0b23023-8a5c-4257-ad6d-d72639f85925%40googlegroups.com.
Regards,Ankush Thakur
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8dbe0176-7fcd-4736-a0ba-78aee3408ac1%40googlegroups.com.
I guess this is the library in question: https://github.com/marcinn/restosaur (took some effort to find it!). Thanks, if I decide to stick with the API-first approach, I'll use it. Either way, I've bookmarked it for future use. :-)
The more strict RESTfull way would be to use the several entry points and use hyperlinks on them (though it would add some extra requests).