Turn from: http://django-rest-framework.org/api-guide/relations.html
Serializer Relations
Bad programmers worry about the code. Good programmers worry about data structures and their relationships.
-linus Torvalds
Relational fields are used to represent model relationships. They can be applied to ForeignKey, Manytomanyfield andonetoonefield relationships, as-as-to-reverse relationships, an D custom relationships such as Genericforeignkey.
Note: The relational fields are declared in relations.py, but by convention your should import them from the Serializersmodule, u Sing from rest_framework import serializers and refer to fields as Serializers.<fieldname>. API Reference
In order to explain the various types of relational fields, we'll use a couple of simple models for our examples. Our models would be a for music albums, and the tracks listed on each album.
Class Album (models. Model):
album_name = models. Charfield (max_length=100)
artist = models. Charfield (max_length=100)
class Track (models. Model):
album = models. ForeignKey (Album, related_name= ' tracks ') Order
= models. Integerfield ()
title = models. Charfield (max_length=100)
duration = models. Integerfield ()
class Meta:
Unique_together = (' album ', ' Order ')
order_by = ' the '
Self): Return
'%d:%s '% (Self.order, self.title)
Relatedfield
Relatedfield May is used to represent the target of the relationship using it ' s __unicode__ method.
For example, the following serializer.
Class Albumserializer (serializers. Modelserializer):
tracks = Relatedfield (many=true)
class Meta:
model = Album
fields = (' Album_name ', ' Artist ', ' tracks ')
Would serialize to the following representation.
{
' album_name ': ' Things We Lost in the Fire ',
' Artist ': ' Low '
tracks ': [
' 1:sunflower ',
' 2: Whitetail ',
' 3:dinosaur Act ',
...
]
}
This field is read only.
Arguments: Many-if applied to a to-many relationship, your should set this argument to True. Primarykeyrelatedfield
Primarykeyrelatedfield May is used to represent the target of the relationship using it ' s primary key.
For example, the following serializer:
Class Albumserializer (serializers. Modelserializer):
tracks = Primarykeyrelatedfield (Many=true, read_only=true)
class Meta:
model = Album
fields = (' Album_name ', ' Artist ', ' tracks ')
Would serialize to a representation as this:
{
' album_name ': ' The Roots ', '
artist ': ' Undun '
tracks ': [M
, N
, M
,
...
]
}
By default This field is Read-write, although your can change this behavior using the READ_ONLY flag.
Arguments: Many-if applied to a to-many relationship, your should set this argument to True. Required-if set to False, the field would accept values of None or the empty-string for nullable relationships. Queryset-by default Modelserializer classes would use the default queryset for the relationship. Serializer classes must either set a Queryset explicitly, or set read_only=true. Hyperlinkedrelatedfield
Hyperlinkedrelatedfield May is used to represent the target of the relationship using a hyperlink.
For example, the following serializer:
class Albumserializer (serializers. Modelserializer): tracks = Hyperlinkedrelatedfield (Many=true, Read_only=true, Vie W_name= ' Track-detail ') class Meta:model = Album fields = (' Album_name ', ' Artist ', ' tracks '