我有一个订单模型,当在序列化程序的create方法中创建条目时,我试图将一个国家添加到many-many字段,但我得到了一个错误:一个名为country的国家已经存在
我有模型:
class Order(models.Model):
name = models.CharField(max_length=150)
description = models.TextField()
price = models.DecimalField(max_digits=10, decimal_places=2, default=0, blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
period = models.DateField(_('СÑок вÑполнениÑ'), blank=True, null=True)
specialization = models.ForeignKey(to='categories.Specialization', on_delete=models.PROTECT, related_name='order_specializations')
country = models.ManyToManyField(Country, related_name='countries', blank=True)
slug = models.SlugField(max_length=255)
customer = models.ForeignKey(User,verbose_name=_("ÐаказÑик"), on_delete=models.CASCADE, related_name='order_customers')
class Country(models.Model):
name = models.CharField(max_length=150, unique=True)
phone_code = models.CharField(max_length=15, blank=True, null=True)
flag = models.FileField(upload_to='flags/', null=True, blank=True)
region = models.ForeignKey(Region, on_delete=models.PROTECT, related_name='countries')
serializers.py
class OrderCreateSerializer(serializers.ModelSerializer):
specialization_id = serializers.IntegerField()
class Meta:
model = Order
fields = ['id', 'name', 'description', 'price', 'created_at', 'specialization_id', 'slug', 'customer', 'file1', 'file2', 'file3', 'country']
read_only_fields = ['customer', 'slug']
country = CountryOrderSerializer(many=True)
def create(self, validated_data):
user = self.context['request'].user
price = validated_data.get('price', 0)
country_data = validated_data.pop('country', [])
if check_customer_balance(customer=user, currency=currency, price=price):
name = validated_data.get('name')
specialization_id = validated_data.pop('specialization_id')
validated_data['specialization'] = Specialization.objects.get(id=specialization_id)
order = super().create(validated_data)
create_order_transfer_money(user, currency, price, order)
else:
return Response({'error': 'Not enough funds'})
for country_info in country_data:
try:
country = Country.objects.get(**country_info)
order.country.add(country_info)
except Country.DoesNotExist:
return Response({'error': 'Country does not exist'})
return order
当我发送邮件请求时
{
"name": "order3",
"description": "ordeadadasd",
"specialization_id": 1,
"country": [{"name": "test"}]
}
create方法甚至没有开始工作。我得到了一个错误:一个名为country的国家已经存在
在我看来,有人试图写入Country表,但它应该添加到与订单相关的表中。根本没有必要在Country表中写任何内容
请帮忙!)