I'm creating a Django application with django-rest-framework and using djongo to connect to MongoDB. I have nested models as such:
class Group(models.Model):
users = models.ArrayModelField(
model_container=User
)
class User(models.Model):
number = models.IntegerField(
default=None,
null=True
)
song = models.EmbeddedModelField(
model_container=Song,
null=True,
blank=True
)
class Meta:
abstract = True
class Song(models.Model):
mp3_file = models.URLField(
default=None,
blank=True,
null=True
)
comments = models.CharField(
max_length=250,
default='',
blank=True
)
class Meta:
abstract = True
When a Group is created, the Users and the Songs are created without any problems. For example, when created, the Group may look like:
{
"name": "Sample",
"users: [
{
"number": null,
"song": {
"mp3_file": null,
"comments": ""
}
}
]
}
This all works fine. However, if I try to do a PUT request and don't change the value of "number", "mp3_file", or "comments", I will get the following error message:
"user": [
{
"number": [
"This field may not be null."
],
"song": {
"mp3_file": [
"This field may not be null."
],
"comments": [
"This field may not be blank."
]
}
}
]
Any thoughts on how to fix this error? I am just using a generic RetrieveUpdateDestroyAPIView as the view for this endpoint.
edit: I have also tried recreating all migrations as well as dropping the table and recreating it, and neither of those approaches helped.
edit: Here is the view:
class GroupDetail(generics.RetrieveUpdateDestroyAPIView):
serializer_class = GroupSerializer
queryset = Group.objects.all()
lookup_field = 'group_name'
lookup_url_kwarg = 'group_name'
And the serializers:
class GroupSerializer(serializers.HyperlinkedModelSerializer):
users = UserSerializer(many=True)
def update(self, instance, validated_data):
if validated_data.get('group_name', None) is not None:
instance.__setattr__('group_name', validated_data.get('group_name'))
instance.save()
return instance
class Meta:
model = Group
fields = (
'group_name',
'users'
)
class UserSerializer(serializers.Serializer):
number = serializers.IntegerField()
song = SongSerializer()
class SongSerializer(serializers.Serializer):
mp3_file = serializers.URLField()
comments = serializers.CharField(
max_length=250
)
But part of the issue is that the serializers are never even being reached since the data is validating incorrectly.
try with change:
class UserSerializer(serializers.Serializer):
number = serializers.IntegerField(allow_null=True)
song = SongSerializer()
class SongSerializer(serializers.Serializer):
mp3_file = serializers.URLField(allow_blank=True)
comments = serializers.CharField(
max_length=250, allow_blank=True
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With