1

将数据发布到发票资源时,我遇到了一个奇怪的问题。如果产品验证失败,发票仍然保存,没有失败的产品数据。

我想要的是在验证所有产品之前不会保存发票。

我是否需要以某种方式处理发票资源中的产品验证,或者美味派可以自动处理这个?

下面的代码......

模型.py

class Invoice(models.Model):
    some_model_fields

class Product(models.Model):
    invoice = models.ForeignKey(Invoice)
    some_model_fields

和forms.py

class ProductForm(ModelForm):
    class Meta:
        model = Product
        exclude = ['invoice']

class InvoiceForm(ModelForm):
    class Meta:
        model = Invoice

和资源.py

class ProductResource(ModelResource):
    invoice = fields.ForeignKey('apps.api.InvoiceResource', 'invoice')

class Meta:
    resource_name = 'product'
    queryset = Product.objects.all()
    validation = FormValidation(form_class=ProductForm)

class InvoiceResource(ModelResource):
    products = fields.ToManyField(ProductResource,
                              'product_set',
                              related_name='invoice',
                              null=False,
                              full=True)

    class Meta:
        queryset = Invoice.objects.all()
        resource_name = 'invoice'
        always_return_data = True
        validation = FormValidation(form_class=InvoiceForm)
4

0 回答 0