Django Admin - 如何从其他表中拖动模型默认字段值 [英] Django Admin - How to pull model default field values from other tables

查看:155
本文介绍了Django Admin - 如何从其他表中拖动模型默认字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从不同的表中自动提取字段值。以下是我的情况:我有两张表:销售和退货。当输入新的退货时,当选择sales_id时,希望显示sales_quantity自动填充,不可编辑(并且也可能希望约束return_quantity< =sales_quantity)。

I am trying to get a field value automatically pulled from different table. Below is my case: I have two tables: sales and returns. While entering a new return, when a "sales_id" is selected, want to show "sales_quantity" automatically populated, non-editable (and also if possible, want to constraint the "return_quantity" <= "sales_quantity").

class T_Sales(models.Model):
    product = models.ForeignKey(P_Product)
    sales_quantity = models.IntegerField()
    def __unicode__(self):             
        return str(self.id)

class T_Return(models.Model):
    sales_id = models.ForeignKey(T_Sales)
    #sales_quantity = models.IntegerField(default=T_Sales.sales_quantity)
    return_quantity = models.IntegerField()
    def __unicode__(self):             
        return self.description


推荐答案

您可以在 T_Return 模型,并将复制值从 T_Sales 保存:

You can create separate field in T_Return model and copy value from T_Sales on save:

class T_Sales(models.Model):
    product = models.ForeignKey(P_Product)
    sales_quantity = models.IntegerField()
    def __unicode__(self):             
        return str(self.id)

class T_Return(models.Model):
    sales_id = models.ForeignKey(T_Sales)
    sales_quantity = models.IntegerField(editable=False)
    return_quantity = models.IntegerField()
    def __unicode__(self):             
        return self.description

    def save(self, *args, **kwargs):
        if not self.sales_quantity:
            self.sales_quantity = self.sales_id.sales_quantity
        supr(T_Return, self).save(*args, **kwargs)

该方法的优点:


  • 仅在(第一个)保存时触发 T_Sales 的附加查询。

  • 在管理员的详细视图中轻松显示价值

  • It will trigger additional query to T_Sales only on (first) save.
  • It is easy to display value in admin's detail view

该方法的缺点是:
- 您正在数据库中存储值
- 如果 T_Sales 对象中的值将更改,则 T_Return 中的值不会自动更改(可以通过保存 T_Sales 的简单触发来修复,但只能在django ORM内)

Cons of that method: - You're storing value in database twice - If value in T_Sales object will change, value in T_Return won't be changed automatically (that can be fixed by simple trigger on save of T_Sales, but only inside django ORM)

如果你不想创建一些验证,你可以覆盖模型的 clean 方法,并在这里做比较,如果有什么问题,你应该抛出$ $ C> ValidationError 。示例:

If you wan't to create some validation, you can override model's clean method and do your comparsion here, if there is something wrong, you should throw ValidationError. Example:

from django.core.exceptions import ValidationError

class T_Return(models.Model):

    # ......

    def clean(self):
        if self.return_quantity > self.sales_quantity:
            raise ValidationError("You can't return more than it was bought")
        return super(T_Return, self).clean()

您还可以将错误分配给 return_quantity sales_quantity 字段,只需将表单'field_name':error 转换为验证错误:

You can also assign error to return_quantity or to sales_quantity field, just pass dict in form 'field_name': error into validation error:

            raiseValidationError({'return_quantity': _('You can't return more than it was bought.')}

将errot分配给 return_quantity sales_quantity 将会显示两次错误。

Assigning that errot both to return_quantity and sales_quantity will show that error twice.

这篇关于Django Admin - 如何从其他表中拖动模型默认字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆