Я пытаюсь создать новый объект, используя модель класса PurchaserShippingDetail , используя экземпляр модели класса Purchaser , но мой способ не работает.
I ' m появляется ошибка
ValueError в / api / clients / shipping / Cannot assign " ":" PurchaserShippingDetail.owner "должен быть экземпляром" Покупатель ".
Модель покупателя файл
class Purchaser(models.Model):
name = models.CharField(max_length=50)
phone = models.CharField(max_length=20, unique=True)
email = models.EmailField(max_length=255, unique=True, blank=True)
image = models.ImageField(default='default.png', upload_to='customer_photos/%Y/%m/%d/')
data_added = models.DateField(default=datetime.date.today)
def __str__(self):
return self.name
PurchaserShippingDetail модель
class purchaserShippingDetail(models.Model):
frequent_customer = models.BooleanField(default=False)
owner = models.OneToOneField(Purchaser, on_delete=models.CASCADE, related_name="purchaser_shipping")
address = models.CharField(max_length=12, blank=True)
zip_code = models.CharField(max_length=12, blank=True)
description = models.TextField(blank=True)
def __str__(self):
return self.owner.name
views.py файл
class purchaserShippingDetailsListCreateView(ListCreateAPIView):
serializer_class = purchaserShippingDetailSerializer
queryset = purchaserShippingDetail.objects.all()
def perform_create(self, serializer):
user = self.request.user
serializer.save(owner=user)
serializers.py
class purchaserShippingDetailSerializer(serializers.ModelSerializer):
owner = serializers.StringRelatedField(read_only=True)
class Meta:
model = purchaserShippingDetail
fields = '__all__'