Я новичок в python Django rest-framework, я сталкиваюсь с этой проблемой при создании нового адреса:
null value in column "user_id" violates not-null constraint
DETAIL: Failing row contains (21, full name, 123456789, any, any, any, any, any, any, f, null).
Это модель адреса:
from django.db import models
from django.contrib.auth.models import User
class UserAddress (models.Model):
user = models.ForeignKey(
User, related_name='addresses', on_delete=models.CASCADE)
full_name = models.TextField(default='')
phone = models.CharField(max_length=30, default='')
city = models.TextField()
province = models.TextField()
street = models.TextField()
description = models.TextField()
postcode = models.CharField(max_length=20)
country = models.CharField(max_length=100)
is_default = models.BooleanField(default=True)
class Meta:
db_table = 'user_addresses'
А это сериализатор:
from rest_framework import serializers
from user_action.models.address import UserAddress
class UserAddressSerializer(serializers.ModelSerializer):
id = serializers.IntegerField(read_only=True)
class Meta:
model = UserAddress
fields = ['id', 'full_name', 'phone', 'city', 'province',
'street', 'description', 'postcode', 'country', 'is_default']
И метод POST:
@api_view(['POST'])
@permission_classes((IsAuthenticated,))
def createUserAddress(request):
user = request.user
if request.method == 'POST':
serializer = UserAddressSerializer(data=request.data)
if serializer.is_valid():
newAddress = serializer.save()
else:
return Response(serializer.errors)
return Response(serializer.data)
Заранее спасибо.