I noticed in the example REST framework site here http://restframework.herokuapp.com/snippets/ that there is a field called 'url' for each user which conveniently links to the user details page. Where on the Django REST documentation is their an example on how to achieve this, or can someone provide me with an example?
2 Answers
Use serializers.HyperlinkedModelSerializer and add 'url this should add the details part in.
Link: serializers.HyperlinkedModelSerializer
UPDATE:
You can add the ID in with the HyperlinkedModelSerializer just add....
id = serializers.Field()
Done :)
3 Comments
jason
tried that but I then get KeyError on 'id' why can not use the pk in fields now?
Glyn Jackson
the docs say it does not include the pk field by default. not sure how to add it in myself sorry.
Glyn Jackson
Updated to add in id field.
There's an entire page of the tutorial dedicated to this topic: http://django-rest-framework.org/tutorial/5-relationships-and-hyperlinked-apis.html
Check the section marked "Hyperlinking our API". It shows the code that defines the JSON you see in that page:
class SnippetSerializer(serializers.HyperlinkedModelSerializer):
owner = serializers.Field(source='owner.username')
highlight = serializers.HyperlinkedIdentityField(view_name='snippet-highlight', format='html')
class Meta:
model = models.Snippet
fields = ('url', 'highlight', 'owner',
'title', 'code', 'linenos', 'language', 'style')
6 Comments
Glyn Jackson
the Django rest framework documentation is very well written. I'm new to Django myself and found them very useful, clear and complete. I personally use django rest framework because of the web browseable part, meaning I can hand off the API to my app guys without having to write a ton of documentation myself. Tastypie is very good (from what I have used so far) but depending on circumstances its important to evaluate these things.
Tom Christie
What's your issue with the REST framework docs? We've put stacks of effort into them, and from where I stand I think they're absolutely top class. Anything in particular you think needs addressing?
mariano
Hello @Tom, my comment read as an attack on your work, and I apologize for that. I've deleted the opinion. The frustration that resulted from my short experience with rest-framework was in fact a consequence of me being a complete novice on the subject, and the docs making a few assumptions about what the readers know on the subject. Of course, it's not your job to write a course on the rest architecture. Again, I apologize.
Tom Christie
@Mariano - No problem, will bear that in mind. It's a bit of a balancing act trying to get the tone of the docs right for as many folks as possible.
Rikki
In this very particular instance, I think it's a little unclear that the Serializer will only display 'url' if you put it in the fields tuple. I guess there's some difference in what I interpreted includes as and what @TomChristie means (as his understanding is affected by knowledge of the internal functioning).
|