Hey all, I got a proper Mystery Error that has me completely stumped.
I am running a Django 4.2.6 + DRF 3.14 backend and there are a few fields that just behave in a way that I believe are explicitly in direct conflict with how I defined them.
I am simplifying this here for better understanding, in reality this is about a whole range of endpoints, but they all derive from common serializers and model field definitions:
Problem: Frontend is making a POST /apiv1/functions/
request with body { "name": "Some Function" }
. They are getting a HTTP 400 “Bad Request” response saying { "owner": [ "This field is required." ], "contacts": [ "This field is required." ] }
.
This endpoint is handled by a view derived from rest_frameworks.generics.ListCreateAPIView
(no custom logic) and the following serializer:
class FunctionSerializer(NodeSerializer):
contacts = serializers.PrimaryKeyRelatedField(
queryset=models.Contact.objects.all(), allow_null=True
)
class Meta(NodeSerializer.Meta):
model = models.Function
fields = '__all__'
which is derived from
class NodeSerializer(serializers.ModelSerializer):
children = serializers.SerializerMethodField()
parents = serializers.SerializerMethodField()
owner = serializers.PrimaryKeyRelatedField(
queryset=models.Contact.objects.all(), allow_null=True
)
created_by = serializers.SlugRelatedField(slug_field='username', read_only=True)
updated_by = serializers.SlugRelatedField(slug_field='username', read_only=True)
class Meta:
model = models.DependencyModelNode
exclude = (
'obj_is_deleted',
'obj_tenant',
'obj_access_scope',
'obj_owner',
)
The model models.Function
in question is:
class Function(DependencyModelNode):
...
contacts = models.ForeignKey(
Contact,
on_delete=models.RESTRICT,
related_name='related_functions',
null=True,
blank=True,
)
....
with no custom save logic, based on a DependencyModelNode
multi-table inheritance parent model:
class DependencyModelNode(models.Model):
name = models.CharField(max_length=100, blank=False)
description = models.TextField(null=True, blank=True)
node_type = models.CharField(max_length=100, null=True, blank=True)
node_active = models.BooleanField(default=False, blank=True)
owner = models.ForeignKey(
to='Contact',
on_delete=models.SET_NULL,
null=True,
blank=True,
related_name='%(app_label)s_%(class)s_owned',
)
...
As you can see, the owner
and contacts
fields are defined in two different places, but are defined to allow null and blank, and the serializers involve explicitly set allow_null=True
as well.
I am at my wit’s end with this - why are these fields erroring as “required” when they clearly (?) should not be?
Thanks in advance for any pointers!