Skip to content

Commit a7bf595

Browse files
committed
Fix hierarchy of the relations page
1 parent b1ffd06 commit a7bf595

1 file changed

Lines changed: 24 additions & 24 deletions

File tree

docs/api-guide/relations.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Relational fields are used to represent model relationships. They can be applie
4242

4343
would solve the issue.
4444

45-
#### Inspecting relationships.
45+
## Inspecting relationships.
4646

4747
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.
4848

@@ -56,7 +56,7 @@ To do so, open the Django shell, using `python manage.py shell`, then import the
5656
name = CharField(allow_blank=True, max_length=100, required=False)
5757
owner = PrimaryKeyRelatedField(queryset=User.objects.all())
5858

59-
# API Reference
59+
## API Reference
6060

6161
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.
6262

@@ -77,7 +77,7 @@ In order to explain the various types of relational fields, we'll use a couple o
7777
def __str__(self):
7878
return '%d: %s' % (self.order, self.title)
7979

80-
## StringRelatedField
80+
### StringRelatedField
8181

8282
`StringRelatedField` may be used to represent the target of the relationship using its `__str__` method.
8383

@@ -109,7 +109,7 @@ This field is read only.
109109

110110
* `many` - If applied to a to-many relationship, you should set this argument to `True`.
111111

112-
## PrimaryKeyRelatedField
112+
### PrimaryKeyRelatedField
113113

114114
`PrimaryKeyRelatedField` may be used to represent the target of the relationship using its primary key.
115115

@@ -145,7 +145,7 @@ By default this field is read-write, although you can change this behavior using
145145
* `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.
146146

147147

148-
## HyperlinkedRelatedField
148+
### HyperlinkedRelatedField
149149

150150
`HyperlinkedRelatedField` may be used to represent the target of the relationship using a hyperlink.
151151

@@ -194,7 +194,7 @@ By default this field is read-write, although you can change this behavior using
194194
* `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`.
195195
* `format` - If using format suffixes, hyperlinked fields will use the same format suffix for the target unless overridden by using the `format` argument.
196196

197-
## SlugRelatedField
197+
### SlugRelatedField
198198

199199
`SlugRelatedField` may be used to represent the target of the relationship using a field on the target.
200200

@@ -235,7 +235,7 @@ When using `SlugRelatedField` as a read-write field, you will normally want to e
235235
* `many` - If applied to a to-many relationship, you should set this argument to `True`.
236236
* `allow_null` - If set to `True`, the field will accept values of `None` or the empty string for nullable relationships. Defaults to `False`.
237237

238-
## HyperlinkedIdentityField
238+
### HyperlinkedIdentityField
239239

240240
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:
241241

@@ -265,15 +265,15 @@ This field is always read-only.
265265

266266
---
267267

268-
# Nested relationships
268+
## Nested relationships
269269

270270
As opposed to previously discussed _references_ to another entity, the referred entity can instead also be embedded or _nested_
271271
in the representation of the object that refers to it.
272272
Such nested relationships can be expressed by using serializers as fields.
273273

274274
If the field is used to represent a to-many relationship, you should add the `many=True` flag to the serializer field.
275275

276-
## Example
276+
### Example
277277

278278
For example, the following serializer:
279279

@@ -311,7 +311,7 @@ Would serialize to a nested representation like this:
311311
],
312312
}
313313

314-
## Writable nested serializers
314+
### Writable nested serializers
315315

316316
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:
317317

@@ -351,7 +351,7 @@ By default nested serializers are read-only. If you want to support write-operat
351351

352352
---
353353

354-
# Custom relational fields
354+
## Custom relational fields
355355

356356
In rare cases where none of the existing relational styles fit the representation you need,
357357
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
363363

364364
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.
365365

366-
## Example
366+
### Example
367367

368368
For example, we could define a relational field to serialize a track to a custom string representation, using its ordering, title, and duration:
369369

@@ -396,7 +396,7 @@ This custom field would then serialize to the following representation:
396396

397397
---
398398

399-
# Custom hyperlinked fields
399+
## Custom hyperlinked fields
400400

401401
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.
402402

@@ -417,7 +417,7 @@ The return value of this method should the object that corresponds to the matche
417417

418418
May raise an `ObjectDoesNotExist` exception.
419419

420-
## Example
420+
### Example
421421

422422
Say we have a URL for a customer object that takes two keyword arguments, like so:
423423

@@ -455,9 +455,9 @@ Generally we recommend a flat style for API representations where possible, but
455455

456456
---
457457

458-
# Further notes
458+
## Further notes
459459

460-
## The `queryset` argument
460+
### The `queryset` argument
461461

462462
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.
463463

@@ -467,7 +467,7 @@ This behavior is now replaced with *always* using an explicit `queryset` argumen
467467

468468
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.
469469

470-
## Customizing the HTML display
470+
### Customizing the HTML display
471471

472472
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.
473473

@@ -477,7 +477,7 @@ To provide customized representations for such inputs, override `display_value()
477477
def display_value(self, instance):
478478
return 'Track: %s' % (instance.title)
479479

480-
## Select field cutoffs
480+
### Select field cutoffs
481481

482482
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.
483483

@@ -498,7 +498,7 @@ In cases where the cutoff is being enforced you may want to instead use a plain
498498
style={'base_template': 'input.html'}
499499
)
500500

501-
## Reverse relations
501+
### Reverse relations
502502

503503
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:
504504

@@ -520,7 +520,7 @@ If you have not set a related name for the reverse relationship, you'll need to
520520

521521
See the Django documentation on [reverse relationships][reverse-relationships] for more details.
522522

523-
## Generic relationships
523+
### Generic relationships
524524

525525
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.
526526

@@ -594,7 +594,7 @@ Note that reverse generic keys, expressed using the `GenericRelation` field, can
594594

595595
For more information see [the Django documentation on generic relations][generic-relations].
596596

597-
## ManyToManyFields with a Through Model
597+
### ManyToManyFields with a Through Model
598598

599599
By default, relational fields that target a ``ManyToManyField`` with a
600600
``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
607607

608608
---
609609

610-
# Third Party Packages
610+
## Third Party Packages
611611

612612
The following third party packages are also available.
613613

614-
## DRF Nested Routers
614+
### DRF Nested Routers
615615

616616
The [drf-nested-routers package][drf-nested-routers] provides routers and relationship fields for working with nested resources.
617617

618-
## Rest Framework Generic Relations
618+
### Rest Framework Generic Relations
619619

620620
The [rest-framework-generic-relations][drf-nested-relations] library provides read/write serialization for generic foreign keys.
621621

0 commit comments

Comments
 (0)