0
我不斷收到這個響應誤差不管我怎麼努力。該方法似乎完美適用於ManyToOne關係,但在OneToOne中失敗。請幫助,如果有人遇到並解決過這樣的:Django的Tastypie - OneToOne關係POST不起作用
400 BAD REQUEST
{"error": "The 'order' field has no data and doesn't allow a default or null value."}
調試試驗:
- 試圖在物流詳情增加一個獨立的主鍵「ID」,因爲我懷疑tastypie可能會失敗,因爲它在創建相關字段之前無法生成Order ID。
- 刪除的訂單字段中DeliveryDetails API,並試圖引用只在訂單資源的相關領域。
請求:
curl -X "POST" "http://localhost:5000/core/api/v1/order/" \
-H "Content-Type: application/json" \
-d "{\"source\":\"W\",\"delivery_detail\":{\"email\":\"[email protected]\"}}"
型號:
class Order(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
created_at = models.DateTimeField(
verbose_name='created at',
auto_now_add=True
)
updated_at = models.DateTimeField(
verbose_name='updated at',
auto_now=True
)
SOURCE_TYPES = (
('I', 'iOS'),
('A', 'Android'),
('W', 'Web'),
('N', 'Not Applicable')
)
source = models.CharField(
verbose_name='order source',
max_length=1,
choices=SOURCE_TYPES,
default='N'
)
class DeliveryDetail(models.Model):
order = models.OneToOneField(
to=Order,
primary_key=True
)
email = models.EmailField(
verbose_name='email address',
max_length=255,
blank=True,
null=True
)
API:
class OrderResource(ModelResource):
delivery_detail = fields.ToOneField(
to='core.api.v1_order.DeliveryDetailResource',
attribute='deliverydetail',
related_name='order',
help_text='Delivery details',
full=True
)
class Meta:
queryset = Order.objects.all()
resource_name = 'order'
class DeliveryDetailResource(ModelResource):
order = fields.ToOneField(
to=OrderResource,
attribute='order'
)
class Meta:
queryset = DeliveryDetail.objects.all()
resource_name = 'delivery_detail'