Django管理员ModelForm,保存计算的相关内容

时间:2017-09-04 14:55:58

标签: python django admin

我在覆盖模型ModelForm的save()方法中遇到django admin的问题。

models.py

class Reservation(models.Model):

    status = models.PositiveSmallIntegerField(choices=STATUS, default=0)
    creation_date = models.DateTimeField(auto_now_add=True)
    date_start = models.DateField()
    date_end = models.DateField()
    service = models.ForeignKey(Service, on_delete=models.CASCADE)

class ReservationItem(models.Model):
    quantity = models.IntegerField()
    unit_price = models.DecimalField(max_digits=10, decimal_places=2)
    unit_tax = models.DecimalField(max_digits=10, decimal_places=2)
    row_total = models.DecimalField(max_digits=10, decimal_places=2)
    row_total_incl_tax = models.DecimalField(max_digits=10, decimal_places=2)
    reservation = models.ForeignKey(Reservation, on_delete=models.CASCADE, related_name='items')
    resource = models.ForeignKey(Resource, on_delete=models.CASCADE)

预订项目是通过可用性查询从服务派生的,因此它为管理员编写了自定义ModelForm。

admin.py

class ReservationModelForm(forms.ModelForm):
    def clean(self):
        if 'service' in self.cleaned_data:
            self._check_availability()

        return self.cleaned_data

    def _check_availability(self):
        '''do some stuff an get items ad an array of ReservationItems instances created like this:
        ReservationItem(
            resource=avail_resource,
            quantity=resource_type.quantity,
            unit_price=resource.unit_price,
            unit_tax=resource.unit_price*resource.tax.percentage,
            row_total=resource.unit_price*resource_type.quantity,
            row_total_incl_tax=...
        )'''
        self.cleaned_data['items'] = items

    # then the save method
    def save(self, commit=True):
        reservation = super(ReservationModelForm, self).save(commit=commit)
        if not self.instance.id:
            service = self.cleaned_data.get('service')
            ....
            reservation.items.set(self.cleaned_data['items'], bulk=False)

         return reservation

class ReservationAdmin(admin.ModelAdmin):
   form = ReservationModelForm

现在我得到的只是一个错误:save() prohibited to prevent data loss due to unsaved related object 'reservation'

但我不能先保存预约,因为我需要原子保存,我该如何解决?

1 个答案:

答案 0 :(得分:0)

我发现我找到了解决方案。 我已经为Reservation模型temp_items添加了一个属性,并使用事务将它们保存在模型save()方法中,这样我就可以提取并验证ModelForm中的项目save()并实际保存在Model save()中方法。 它看起来很棒