使用应用模型循环导入

时间:2017-06-12 14:32:21

标签: python django

我有两个不同型号的应用程序,错误是“无法导入名称sku'”,我认为这是因为循环导入。如何在不引用模型导入的情况下仍然引用此app.model。

Model PurchaseOrderDetail App Purchase_order

from product.models import InventoryTransaction


class PurchaseOrderDetail(AuditMixin, models.Model):
    purchase_order = models.ForeignKey(PurchaseOrder, 
    on_delete=models.PROTECT)
    sku = models.ForeignKey(Sku, on_delete=models.PROTECT)
    qty_received = models.IntegerField(default=0, null=True, blank=True)
    reason_code = models.IntegerField(default=0, null=True, blank=True)

    def _get_sku_code(self):
        return self.sku.sku_code

    def _set_sku_code(self, value):
        tenant = self.purchase_order.tenant
        sku = Sku.objects.get(tenant=tenant, sku_code=value)
        self.sku = sku

    sku_code = property(_get_sku_code, _set_sku_code)

    def calc_received(self):
        # calculate sum of qty_received from inventory_transactions
        sum_quantity = InventoryTransaction.objects.filter(
            sku=self.sku,
            po_number=self.purchase_order,
            transaction_type='100'
        ).aggregate(
            quantity_sum=Sum(F('quantity'))
        )
        return sum_quantity

模型库存交易 应用产品

from purchase_order.models import PurchaseOrderDetail


class InventoryTransaction(models.Model):
    tenant = models.ForeignKey(Tenant)
    po_number = models.CharField(max_length=100)
    inventory_transaction = models.ForeignKey(PurchaseOrderInventoryTransaction)

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)
        # update the PO detail with the qty_received
        obj = PurchaseOrderDetail.objects.get(
            purchase_order=self.po_number,
            sku=self.inventory_transaction.sku
        )
        obj.qty_received = obj.calc_received()
        obj.save()

1 个答案:

答案 0 :(得分:1)

如果您不希望出现此问题,只需在def save功能中导入PurchaseOrderDetails。

def save(self, *args, **kwargs): from purchase_order.models import PurchaseOrderDetail ...