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
I have reduced the issue to the simplest possible case.
Created a clean test project.
I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)
Not yet!
Steps to reproduce
models.py
from django.db import models
class Person(models.Model):
name = models.CharField(
max_length=100,
unique=True
)
class PersonExtension(models.Model):
person = models.OneToOneField(
Person
)
extra_data = models.CharField(
max_length=100,
default="",
blank=True
)
serializers.py
from rest_framework.serializers import HyperlinkedModelSerializer
from prueba import models
class PersonSerializer(HyperlinkedModelSerializer):
class Meta:
model = models.Person
fields = (
'url',
'id',
'name'
)
class PersonExtensionSerializer(HyperlinkedModelSerializer):
person = PersonSerializer()
class Meta:
model = models.PersonExtension
fields = (
'url',
'id',
'person',
'extra_data'
)
def create(self, validated_data):
person_data = validated_data.pop('person')
person, created = models.Person.objects.update_or_create(
pk=person_data.get('id'),
defaults=person_data
)
validated_data['person'] = person
person_extension = models.PersonExtension.objects.create(
**validated_data
)
return person_extension
def update(self, instance, validated_data):
person_data = validated_data.get('person')
instance.person.name = person_data.get(
'name',
instance.person.name
)
instance.person.save()
instance.extra_data = validated_data.get(
'extra_data',
instance.extra_data
)
instance.save()
return instance
views.py
from rest_framework.viewsets import ModelViewSet
from prueba import models, serializers
class PersonViewSet(ModelViewSet):
queryset = models.Person.objects.all()
serializer_class = serializers.PersonSerializer
class PersonExtensionViewSet(ModelViewSet):
queryset = models.PersonExtension.objects.all()
serializer_class = serializers.PersonExtensionSerializer
urls.py
from django.conf.urls import url, include
from rest_framework.routers import DefaultRouter
from prueba import views
router = DefaultRouter()
router.register(r'persons', views.PersonViewSet)
router.register(r'personextensions', views.PersonExtensionViewSet)
urlpatterns = [
url(r'^', include(router.urls)),
]
We can't automagically handle unique constraints on nested serializers. Specify the name field on the serializer explicitly (so that it doesn't magically include the validator). You'll then need to enforce the uniqueness explicitly instead (eg in the .validate() method of the parent serializer)
Either specify the name explicitely on the serializer or use the extra_kwargs to get rid of the unique validator. Either way work and it's not different from #2996 or you have a different issue
Checklist
master
branch of Django REST framework.Did a clean pyvenv-3.5 workspace and git cloned master.
Couldn't find anything, but perhaps I'm describing wrong my issue.
Created a clean test project.
Not yet!
Steps to reproduce
models.py
serializers.py
views.py
urls.py
Expected behavior
POST to localhost:8000/personextensions/
Person and PersonExtension are both created successfully.
PUT to localhost:8000/personextensions/1/
extra_data successfully modified to "How are you?"
PUT to localhost:8000/personextensions/1/
Successfully changed name to "Hello 1" and extra_data to "How are you?".
Actual behavior
POST to localhost:8000/personextensions/
Person and PersonExtension are both created successfully.
PUT to localhost:8000/personextensions/1/
Error: "Person with this name already exists."
PUT to localhost:8000/personextensions/1/
Successfully changed name to "Hello 1" and extra_data to "How are you?".
Thanks in advance!
The text was updated successfully, but these errors were encountered: