mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-08 06:14:47 +03:00
Partial update to remove the ModelSerializer
from the `Serializer section
This commit is contained in:
parent
2cec5ee8aa
commit
9fd838e556
|
@ -296,19 +296,26 @@ Similarly, the `.validated_data` property will include nested data structures.
|
||||||
If you're supporting writable nested representations you'll need to write `.create()` or `.update()` methods that handle saving multiple objects.
|
If you're supporting writable nested representations you'll need to write `.create()` or `.update()` methods that handle saving multiple objects.
|
||||||
|
|
||||||
The following example demonstrates how you might handle creating a user with a nested profile object.
|
The following example demonstrates how you might handle creating a user with a nested profile object.
|
||||||
|
Please note that though we're using Django's Models it could be any Python class as well.
|
||||||
|
|
||||||
class UserSerializer(serializers.ModelSerializer):
|
|
||||||
profile = ProfileSerializer()
|
|
||||||
|
|
||||||
class Meta:
|
class CommentSerializer(serializers.Serializer):
|
||||||
model = User
|
user = UserSerializer(required=False) # May be an anonymous user.
|
||||||
fields = ('username', 'email', 'profile')
|
content = serializers.CharField(max_length=200)
|
||||||
|
created = serializers.DateTimeField()
|
||||||
|
|
||||||
def create(self, validated_data):
|
def create(self, validated_data):
|
||||||
profile_data = validated_data.pop('profile')
|
# Get or create the user if provided
|
||||||
user = User.objects.create(**validated_data)
|
user = None
|
||||||
Profile.objects.create(user=user, **profile_data)
|
user_data = validated_data.pop('user')
|
||||||
return user
|
if user_data:
|
||||||
|
user = User.objects.get_or_create(**user_data)
|
||||||
|
|
||||||
|
# Create the comment
|
||||||
|
comment = Comment.objects.create(user=user, **validated_data)
|
||||||
|
|
||||||
|
return comment
|
||||||
|
|
||||||
|
|
||||||
#### Writing `.update()` methods for nested representations
|
#### Writing `.update()` methods for nested representations
|
||||||
|
|
||||||
|
@ -319,9 +326,10 @@ For updates you'll want to think carefully about how to handle updates to relati
|
||||||
* Ignore the data and leave the instance as it is.
|
* Ignore the data and leave the instance as it is.
|
||||||
* Raise a validation error.
|
* Raise a validation error.
|
||||||
|
|
||||||
Here's an example for an `update()` method on our previous `UserSerializer` class.
|
Here's an example for an `update()` method on our previous `CommentSerializer` class.
|
||||||
|
|
||||||
def update(self, instance, validated_data):
|
def update(self, instance, validated_data):
|
||||||
|
|
||||||
profile_data = validated_data.pop('profile')
|
profile_data = validated_data.pop('profile')
|
||||||
# Unless the application properly enforces that this field is
|
# Unless the application properly enforces that this field is
|
||||||
# always set, the follow could raise a `DoesNotExist`, which
|
# always set, the follow could raise a `DoesNotExist`, which
|
||||||
|
|
Loading…
Reference in New Issue
Block a user