Skip to content

Commit 9ba29a8

Browse files
committed
Finalizing 3.0 release notes
1 parent 8e549a7 commit 9ba29a8

File tree

2 files changed

+14
-26
lines changed

2 files changed

+14
-26
lines changed

docs/api-guide/serializers.md

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -567,21 +567,22 @@ There needs to be a way of determining which views should be used for hyperlinki
567567

568568
By default hyperlinks are expected to correspond to a view name that matches the style `'{model_name}-detail'`, and looks up the instance by a `pk` keyword argument.
569569

570-
You can change the field that is used for object lookups by setting the `lookup_field` option. The value of this option should correspond both with a kwarg in the URL conf, and with a field on the model. For example:
570+
You can override a URL field view name and lookup field by using either, or both of, the `view_name` and `lookup_field` options in the `extra_field_kwargs` setting, like so:
571571

572572
class AccountSerializer(serializers.HyperlinkedModelSerializer):
573573
class Meta:
574574
model = Account
575-
fields = ('url', 'account_name', 'users', 'created')
576-
lookup_field = 'slug'
577-
578-
Note that the `lookup_field` will be used as the default on *all* hyperlinked fields, including both the URL identity, and any hyperlinked relationships.
575+
fields = ('account_url', 'account_name', 'users', 'created')
576+
extra_field_kwargs = {
577+
'url': {'view_name': 'accounts', 'lookup_field': 'account_name'}
578+
'users': {'lookup_field': 'username'}
579+
}
579580

580-
For more specific requirements such as specifying a different lookup for each field, you'll want to set the fields on the serializer explicitly. For example:
581+
Alternatively you can set the fields on the serializer explicitly. For example:
581582

582583
class AccountSerializer(serializers.HyperlinkedModelSerializer):
583584
url = serializers.HyperlinkedIdentityField(
584-
view_name='account-detail',
585+
view_name='accounts',
585586
lookup_field='slug'
586587
)
587588
users = serializers.HyperlinkedRelatedField(
@@ -595,28 +596,15 @@ For more specific requirements such as specifying a different lookup for each fi
595596
model = Account
596597
fields = ('url', 'account_name', 'users', 'created')
597598

598-
## Overriding the URL field behavior
599-
600-
The name of the URL field defaults to 'url'. You can override this globally, by using the `URL_FIELD_NAME` setting.
601-
602-
You can also override this on a per-serializer basis by using the `url_field_name` option on the serializer, like so:
599+
---
603600

604-
class AccountSerializer(serializers.HyperlinkedModelSerializer):
605-
class Meta:
606-
model = Account
607-
fields = ('account_url', 'account_name', 'users', 'created')
608-
url_field_name = 'account_url'
601+
**Tip**: Properly matching together hyperlinked representations and your URL conf can sometimes be a bit fiddly. Printing the `repr` of a `HyperlinkedModelSerializer` instance is a particularly useful way to inspect exactly which view names and lookup fields the relationships are expected to map too.
609602

610-
**Note**: The generic view implementations normally generate a `Location` header in response to successful `POST` requests. Serializers using `url_field_name` option will not have this header automatically included by the view. If you need to do so you will ned to also override the view's `get_success_headers()` method.
603+
---
611604

612-
You can also override the URL field's view name and lookup field without overriding the field explicitly, by using the `view_name` and `lookup_field` options, like so:
605+
## Changing the URL field name
613606

614-
class AccountSerializer(serializers.HyperlinkedModelSerializer):
615-
class Meta:
616-
model = Account
617-
fields = ('account_url', 'account_name', 'users', 'created')
618-
view_name = 'account_detail'
619-
lookup_field='account_name'
607+
The name of the URL field defaults to 'url'. You can override this globally, by using the `URL_FIELD_NAME` setting.
620608

621609
---
622610

docs/topics/3.0-announcement.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ The `view_name` and `lookup_field` options have been moved to `PendingDeprecatio
329329
model = MyModel
330330
fields = ('url', 'email', 'notes', 'is_admin')
331331
extra_kwargs = {
332-
'url': {'lookup_field': 'uuid'}
332+
'url': {'lookup_field': 'uuid'}
333333
}
334334

335335
Alternatively, specify the field explicitly on the serializer class:

0 commit comments

Comments
 (0)