如何访问admin.TabularInline上的字段模型

时间:2016-04-16 06:35:16

标签: python django

我是django的新手,我想构建一个名为“Recycle Bank”的应用程序。

这是我的models.py

class Waste(models.Model):
    name = models.CharField(max_length=20)
    wtype = models.ForeignKey(WasteType)
    specification = models.TextField(blank=True)
    purchasing_price = models.DecimalField(max_digits=8, decimal_places=2)
    selling_price = models.DecimalField(max_digits=8, decimal_places=2)

class Purchasing(models.Model):
    timestamp = models.DateTimeField(auto_now=True)
    client = models.ForeignKey(Client)
    officer = models.ForeignKey(Officer)
    waste = models.ManyToManyField(Waste, through='PurchasingDetail')

    def __str__(self):
        return self.client.user.username

class PurchasingDetail(models.Model):
    waste = models.ForeignKey(Waste)
    purchasing = models.ForeignKey(Purchasing)
    weight = models.DecimalField(max_digits=8, decimal_places=2)
    price = models.DecimalField(max_digits=8, decimal_places=2)

这是我的admin.py

class PurchasingDetailInlineAdmin(admin.TabularInline):
    model = PurchasingDetail
    extra = 1

class PurchasingAdmin(admin.ModelAdmin):
    list_display = ['client', 'timestamp', 'officer',]
    fields = ('client',)
    inlines = [PurchasingDetailInlineAdmin,]

    def save_model(self, request, obj, form, change):
        obj.officer_id = request.user.id
        obj.save()

所以,我想在price中使用指定的计数设置PurchasingDetail(models.Model)对象(在这种情况下:Waste.purchasing_price * PurchasingDetail.weight)。我知道我必须像访问PurchasingDetail(models.Model)一样访问save_model()对象。怎么做?

这是我的管理界面截图https://drive.google.com/open?id=0B2dFcp9QmLL2X2duVHVfeVNHNlU

谢谢。

0 个答案:

没有答案