Django Rest Framework Association Relationship--serializer Relations__django

Source: Internet
Author: User

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 ' 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.