Исправлена обработка ForeignKey полей в DraftOrderService
Добавлена правильная обработка ForeignKey полей (customer, delivery_address, pickup_shop) при обновлении черновика. Теперь ID конвертируются в объекты моделей перед присваиванием. Исправляет ошибку: Cannot assign "1": "Order.customer" must be a "Customer" instance. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -82,15 +82,37 @@ class DraftOrderService:
|
|||||||
raise ValidationError("Можно обновлять только черновики заказов")
|
raise ValidationError("Можно обновлять только черновики заказов")
|
||||||
|
|
||||||
# Обновляем только переданные поля
|
# Обновляем только переданные поля
|
||||||
updatable_fields = [
|
# ForeignKey поля требуют специальной обработки
|
||||||
'customer', 'is_delivery', 'delivery_address', 'pickup_shop',
|
fk_fields = {
|
||||||
'delivery_date', 'delivery_time_start', 'delivery_time_end',
|
'customer': 'customers.Customer',
|
||||||
|
'delivery_address': 'customers.Address',
|
||||||
|
'pickup_shop': 'shops.Shop',
|
||||||
|
}
|
||||||
|
|
||||||
|
simple_fields = [
|
||||||
|
'is_delivery', 'delivery_date', 'delivery_time_start', 'delivery_time_end',
|
||||||
'delivery_cost', 'payment_method', 'customer_is_recipient',
|
'delivery_cost', 'payment_method', 'customer_is_recipient',
|
||||||
'recipient_name', 'recipient_phone', 'is_anonymous',
|
'recipient_name', 'recipient_phone', 'is_anonymous',
|
||||||
'special_instructions', 'discount_amount'
|
'special_instructions', 'discount_amount'
|
||||||
]
|
]
|
||||||
|
|
||||||
for field in updatable_fields:
|
# Обрабатываем ForeignKey поля
|
||||||
|
for field_name, model_path in fk_fields.items():
|
||||||
|
if field_name in data and data[field_name]:
|
||||||
|
# Получаем модель
|
||||||
|
app_label, model_name = model_path.split('.')
|
||||||
|
from django.apps import apps
|
||||||
|
Model = apps.get_model(app_label, model_name)
|
||||||
|
|
||||||
|
# Получаем объект по ID
|
||||||
|
try:
|
||||||
|
instance = Model.objects.get(pk=data[field_name])
|
||||||
|
setattr(order, field_name, instance)
|
||||||
|
except Model.DoesNotExist:
|
||||||
|
pass # Игнорируем несуществующие объекты
|
||||||
|
|
||||||
|
# Обрабатываем простые поля
|
||||||
|
for field in simple_fields:
|
||||||
if field in data:
|
if field in data:
|
||||||
setattr(order, field, data[field])
|
setattr(order, field, data[field])
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user