@@ -123,9 +123,9 @@ Would serialize to a representation like this:
...
@@ -123,9 +123,9 @@ Would serialize to a representation like this:
'album_name': 'Graceland',
'album_name': 'Graceland',
'artist': 'Paul Simon'
'artist': 'Paul Simon'
'tracks': [
'tracks': [
'http://www.example.com/api/tracks/45',
'http://www.example.com/api/tracks/45/',
'http://www.example.com/api/tracks/46',
'http://www.example.com/api/tracks/46/',
'http://www.example.com/api/tracks/47',
'http://www.example.com/api/tracks/47/',
...
...
]
]
}
}
...
@@ -138,9 +138,7 @@ By default this field is read-write, although you can change this behavior using
...
@@ -138,9 +138,7 @@ By default this field is read-write, although you can change this behavior using
*`many` - If applied to a to-many relationship, you should set this argument to `True`.
*`many` - If applied to a to-many relationship, you should set this argument to `True`.
*`required` - If set to `False`, the field will accept values of `None` or the empty-string for nullable relationships.
*`required` - If set to `False`, the field will accept values of `None` or the empty-string for nullable relationships.
*`queryset` - By default `ModelSerializer` classes will use the default queryset for the relationship. `Serializer` classes must either set a queryset explicitly, or set `read_only=True`.
*`queryset` - By default `ModelSerializer` classes will use the default queryset for the relationship. `Serializer` classes must either set a queryset explicitly, or set `read_only=True`.
*`slug_field` - The field on the target that should be used for the lookup. Default is `'slug'`.
*`lookup_field` - The field on the target that should be used for the lookup. Should correspond to a URL keyword argument on the referenced view. Default is `'pk'`.
*`pk_url_kwarg` - The named url parameter for the pk field lookup. Default is `pk`.
*`slug_url_kwarg` - The named url parameter for the slug field lookup. Default is to use the same value as given for `slug_field`.
*`format` - If using format suffixes, hyperlinked fields will use the same format suffix for the target unless overridden by using the `format` argument.
*`format` - If using format suffixes, hyperlinked fields will use the same format suffix for the target unless overridden by using the `format` argument.
## SlugRelatedField
## SlugRelatedField
...
@@ -196,7 +194,7 @@ Would serialize to a representation like this:
...
@@ -196,7 +194,7 @@ Would serialize to a representation like this:
@@ -291,32 +289,23 @@ This custom field would then serialize to the following representation.
...
@@ -291,32 +289,23 @@ This custom field would then serialize to the following representation.
## Reverse relations
## Reverse relations
Note that reverse relationships are not automatically generated by the `ModelSerializer` and `HyperlinkedModelSerializer` classes. To include a reverse relationship, you cannot simply add it to the fields list.
Note that reverse relationships are not automatically included by the `ModelSerializer` and `HyperlinkedModelSerializer` classes. To include a reverse relationship, you must explicitly add it to the fields list. For example:
**Thefollowing will not work:**
class AlbumSerializer(serializers.ModelSerializer):
class AlbumSerializer(serializers.ModelSerializer):
class Meta:
class Meta:
fields = ('tracks', ...)
fields = ('tracks', ...)
Instead, you must explicitly add it to the serializer. For example:
class AlbumSerializer(serializers.ModelSerializer):
By default, the field will uses the same accessor as it's field name to retrieve the relationship, so in this example, `Album` instances would need to have the `tracks` attribute for this relationship to work.
The best way to ensure this is typically to make sure that the relationship on the model definition has it's `related_name` argument properly set. For example:
You'll normally want to ensure that you've set an appropriate `related_name` argument on the relationship, that you can use as the field name. For example:
class Track(models.Model):
class Track(models.Model):
album = models.ForeignKey(Album, related_name='tracks')
album = models.ForeignKey(Album, related_name='tracks')
...
...
Alternatively, you can use the `source` argument on the serializer field, to use a different accessor attribute than the field name. For example.
If you have not set a related name for the reverse relationship, you'll need to use the automatically generated related name in the `fields` argument. For example:
class AlbumSerializer(serializers.ModelSerializer):
class AlbumSerializer(serializers.ModelSerializer):