I've encountered a weird behavior that I don't quite get. For the model
class Tenant:
name = models.CharField(max_length=155)
country = models.CharField(max_lengh=155)
I'm using this serializer:
class Serializer(serializers.ModelSerializer):
class Meta:
model = Tenant
fields = (name,)
This set up will allow me to save a tenant instance that doesn't have a country supplied even though the null=True
should be enforced on the DB level (Postgres). Only when I add country
to the fields, will it enforce the null=False
constraint.
This seems quite unintuitive to me.
Edit: Here is a simple code example of my problem:
class AppModel(models.Model):
name = models.CharField(max_length=255)
country = models.CharField(max_length=255)
class CrateAppModel(APIView):
def post(self, request, format=None):
serializer = AppModelSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
class AppModelSerializer(serializers.ModelSerializer):
class Meta:
model = AppModel
fields = (
"name",
)
Using simple post requests I'm able to create an instance without a country field. I don't understand why? In my database country
is of type string and empty.