You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api-guide/relations.md
+24-24Lines changed: 24 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,7 +42,7 @@ Relational fields are used to represent model relationships. They can be applie
42
42
43
43
would solve the issue.
44
44
45
-
####Inspecting relationships.
45
+
## Inspecting relationships.
46
46
47
47
When using the `ModelSerializer` class, serializer fields and relationships will be automatically generated for you. Inspecting these automatically generated fields can be a useful tool for determining how to customize the relationship style.
48
48
@@ -56,7 +56,7 @@ To do so, open the Django shell, using `python manage.py shell`, then import the
56
56
name = CharField(allow_blank=True, max_length=100, required=False)
In order to explain the various types of relational fields, we'll use a couple of simple models for our examples. Our models will be for music albums, and the tracks listed on each album.
62
62
@@ -77,7 +77,7 @@ In order to explain the various types of relational fields, we'll use a couple o
77
77
def __str__(self):
78
78
return '%d: %s' % (self.order, self.title)
79
79
80
-
## StringRelatedField
80
+
###StringRelatedField
81
81
82
82
`StringRelatedField` may be used to represent the target of the relationship using its `__str__` method.
83
83
@@ -109,7 +109,7 @@ This field is read only.
109
109
110
110
*`many` - If applied to a to-many relationship, you should set this argument to `True`.
111
111
112
-
## PrimaryKeyRelatedField
112
+
###PrimaryKeyRelatedField
113
113
114
114
`PrimaryKeyRelatedField` may be used to represent the target of the relationship using its primary key.
115
115
@@ -145,7 +145,7 @@ By default this field is read-write, although you can change this behavior using
145
145
*`pk_field` - Set to a field to control serialization/deserialization of the primary key's value. For example, `pk_field=UUIDField(format='hex')` would serialize a UUID primary key into its compact hex representation.
146
146
147
147
148
-
## HyperlinkedRelatedField
148
+
###HyperlinkedRelatedField
149
149
150
150
`HyperlinkedRelatedField` may be used to represent the target of the relationship using a hyperlink.
151
151
@@ -194,7 +194,7 @@ By default this field is read-write, although you can change this behavior using
194
194
*`lookup_url_kwarg` - The name of the keyword argument defined in the URL conf that corresponds to the lookup field. Defaults to using the same value as `lookup_field`.
195
195
*`format` - If using format suffixes, hyperlinked fields will use the same format suffix for the target unless overridden by using the `format` argument.
196
196
197
-
## SlugRelatedField
197
+
###SlugRelatedField
198
198
199
199
`SlugRelatedField` may be used to represent the target of the relationship using a field on the target.
200
200
@@ -235,7 +235,7 @@ When using `SlugRelatedField` as a read-write field, you will normally want to e
235
235
*`many` - If applied to a to-many relationship, you should set this argument to `True`.
236
236
*`allow_null` - If set to `True`, the field will accept values of `None` or the empty string for nullable relationships. Defaults to `False`.
237
237
238
-
## HyperlinkedIdentityField
238
+
###HyperlinkedIdentityField
239
239
240
240
This field can be applied as an identity relationship, such as the `'url'` field on a HyperlinkedModelSerializer. It can also be used for an attribute on the object. For example, the following serializer:
241
241
@@ -265,15 +265,15 @@ This field is always read-only.
265
265
266
266
---
267
267
268
-
# Nested relationships
268
+
##Nested relationships
269
269
270
270
As opposed to previously discussed _references_ to another entity, the referred entity can instead also be embedded or _nested_
271
271
in the representation of the object that refers to it.
272
272
Such nested relationships can be expressed by using serializers as fields.
273
273
274
274
If the field is used to represent a to-many relationship, you should add the `many=True` flag to the serializer field.
275
275
276
-
## Example
276
+
###Example
277
277
278
278
For example, the following serializer:
279
279
@@ -311,7 +311,7 @@ Would serialize to a nested representation like this:
311
311
],
312
312
}
313
313
314
-
## Writable nested serializers
314
+
###Writable nested serializers
315
315
316
316
By default nested serializers are read-only. If you want to support write-operations to a nested serializer field you'll need to create `create()` and/or `update()` methods in order to explicitly specify how the child relationships should be saved:
317
317
@@ -351,7 +351,7 @@ By default nested serializers are read-only. If you want to support write-operat
351
351
352
352
---
353
353
354
-
# Custom relational fields
354
+
##Custom relational fields
355
355
356
356
In rare cases where none of the existing relational styles fit the representation you need,
357
357
you can implement a completely custom relational field, that describes exactly how the
@@ -363,7 +363,7 @@ If you want to implement a read-write relational field, you must also implement
363
363
364
364
To provide a dynamic queryset based on the `context`, you can also override `.get_queryset(self)` instead of specifying `.queryset` on the class or when initializing the field.
365
365
366
-
## Example
366
+
###Example
367
367
368
368
For example, we could define a relational field to serialize a track to a custom string representation, using its ordering, title, and duration:
369
369
@@ -396,7 +396,7 @@ This custom field would then serialize to the following representation:
396
396
397
397
---
398
398
399
-
# Custom hyperlinked fields
399
+
##Custom hyperlinked fields
400
400
401
401
In some cases you may need to customize the behavior of a hyperlinked field, in order to represent URLs that require more than a single lookup field.
402
402
@@ -417,7 +417,7 @@ The return value of this method should the object that corresponds to the matche
417
417
418
418
May raise an `ObjectDoesNotExist` exception.
419
419
420
-
## Example
420
+
###Example
421
421
422
422
Say we have a URL for a customer object that takes two keyword arguments, like so:
423
423
@@ -455,9 +455,9 @@ Generally we recommend a flat style for API representations where possible, but
455
455
456
456
---
457
457
458
-
# Further notes
458
+
##Further notes
459
459
460
-
## The `queryset` argument
460
+
###The `queryset` argument
461
461
462
462
The `queryset` argument is only ever required for *writable* relationship field, in which case it is used for performing the model instance lookup, that maps from the primitive user input, into a model instance.
463
463
@@ -467,7 +467,7 @@ This behavior is now replaced with *always* using an explicit `queryset` argumen
467
467
468
468
Doing so reduces the amount of hidden 'magic' that `ModelSerializer` provides, makes the behavior of the field more clear, and ensures that it is trivial to move between using the `ModelSerializer` shortcut, or using fully explicit `Serializer` classes.
469
469
470
-
## Customizing the HTML display
470
+
###Customizing the HTML display
471
471
472
472
The built-in `__str__` method of the model will be used to generate string representations of the objects used to populate the `choices` property. These choices are used to populate select HTML inputs in the browsable API.
473
473
@@ -477,7 +477,7 @@ To provide customized representations for such inputs, override `display_value()
477
477
def display_value(self, instance):
478
478
return 'Track: %s' % (instance.title)
479
479
480
-
## Select field cutoffs
480
+
###Select field cutoffs
481
481
482
482
When rendered in the browsable API relational fields will default to only displaying a maximum of 1000 selectable items. If more items are present then a disabled option with "More than 1000 items…" will be displayed.
483
483
@@ -498,7 +498,7 @@ In cases where the cutoff is being enforced you may want to instead use a plain
498
498
style={'base_template': 'input.html'}
499
499
)
500
500
501
-
## Reverse relations
501
+
###Reverse relations
502
502
503
503
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:
504
504
@@ -520,7 +520,7 @@ If you have not set a related name for the reverse relationship, you'll need to
520
520
521
521
See the Django documentation on [reverse relationships][reverse-relationships] for more details.
522
522
523
-
## Generic relationships
523
+
###Generic relationships
524
524
525
525
If you want to serialize a generic foreign key, you need to define a custom field, to determine explicitly how you want to serialize the targets of the relationship.
526
526
@@ -594,7 +594,7 @@ Note that reverse generic keys, expressed using the `GenericRelation` field, can
594
594
595
595
For more information see [the Django documentation on generic relations][generic-relations].
596
596
597
-
## ManyToManyFields with a Through Model
597
+
###ManyToManyFields with a Through Model
598
598
599
599
By default, relational fields that target a ``ManyToManyField`` with a
600
600
``through`` model specified are set to read-only.
@@ -607,15 +607,15 @@ If you wish to represent [extra fields on a through model][django-intermediary-m
607
607
608
608
---
609
609
610
-
# Third Party Packages
610
+
##Third Party Packages
611
611
612
612
The following third party packages are also available.
613
613
614
-
## DRF Nested Routers
614
+
###DRF Nested Routers
615
615
616
616
The [drf-nested-routers package][drf-nested-routers] provides routers and relationship fields for working with nested resources.
617
617
618
-
## Rest Framework Generic Relations
618
+
###Rest Framework Generic Relations
619
619
620
620
The [rest-framework-generic-relations][drf-nested-relations] library provides read/write serialization for generic foreign keys.
0 commit comments