DRF 3.0.1: .create () method does not support writeable nested fields
I just upgraded to Django REST Framework 3.0.1 and my tests fail with this add- on to improve checking for nested create and update ( The .create() method does not support writable nestedfields by default.
), but as far as I know, I am not trying to do any nested entries. I have one associated serializer, but it is set to read_only=True
, and when I send that endpoint, the associated objects all already exist. Any ideas or am I just doing something wrong?
class Like(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField(db_index=True)
content_object = generic.GenericForeignKey()
user = models.ForeignKey(settings.AUTH_USER_MODEL)
class Meta:
unique_together = (("user", "content_type", "object_id"),)
class LikeSerializer(serializers.ModelSerializer):
user = UserSerializer(read_only=True, default=serializers.CurrentUserDefault())
class Meta:
model = Like
class LikeViewSet(viewsets.ModelViewSet):
queryset = Like.objects.all()
serializer_class = LikeSerializer
def perform_create(self, serializer):
serializer.save(user=self.request.user)
def perform_update(self, serializer):
serializer.save(user=self.request.user)
printed LikeSerializer
LikeSerializer():
id = IntegerField(label='ID', read_only=True)
user = UserSerializer(default=CurrentUserDefault(), read_only=True):
email = EmailField(
allow_blank=True,
allow_null=True,
max_length=75,
required=False,
validators=[<UniqueValidator(queryset=User.objects.all())>])
id = IntegerField(label='ID', read_only=True)
username = CharField(
help_text='Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.',
max_length=30,
validators=[<django.core.validators.RegexValidator object>,
<UniqueValidator(queryset=User.objects.all())>])
fullname = CharField(allow_blank=True, allow_null=True, max_length=80, required=False)
original_photo = ImageField(allow_null=True, max_length=100, required=False)
about = CharField(allow_blank=True, allow_null=True, required=False, style={'type': 'textarea'})
user_following_count = ReadOnlyField()
user_followers_count = ReadOnlyField()
content_type = SerializerMethodField()
created = DateTimeField(read_only=True)
object_id = IntegerField(required=True)
content_type = PrimaryKeyRelatedField(queryset=ContentType.objects.all(), required=True)
class Meta:
validators = [<UniqueTogetherValidator(queryset=Like.objects.all(), fields=('user', 'content_type', 'object_id'))>]
I just post object_id
and the content_type
object that I like. Worked fine before updating.
source to share
After that as a problem, here's ... https://github.com/tomchristie/django-rest-framework/pull/2196#issuecomment-66839296
source to share