【问题标题】:'QuerySet' object has no attribute ' - Django ORM issue'QuerySet' 对象没有属性' - Django ORM 问题
【发布时间】:2016-05-16 16:36:13
【问题描述】:

我有 3 个模型 物料,UOM,BIN_UOM

    @with_author  
    class Material(models.Model):
        version = IntegerVersionField( )
        code = models.CharField(max_length=30)
        name = models.CharField(max_length=30)
        slug = models.SlugField(max_length=80, blank=True)
        description = models.TextField(null=True, blank=True)
        materialuom = models.CharField(max_length=1,
                                  choices=UOM_CHOICES)
        creation_time = models.DateTimeField(auto_now_add=True, blank=True)
        itemgroup = models.ForeignKey(ItemGroup, on_delete=models.PROTECT)
        keywords = models.CharField(max_length=50,null=True, blank=True)
        valid_from = models.DateTimeField(null=True, blank=True)
        valid_to = models.DateTimeField(null=True, blank=True)
        min_quantity = models.DecimalField(max_digits=19, decimal_places=10)
        trigger_quantity = models.DecimalField(max_digits=19, decimal_places=10)
        max_quantity = models.DecimalField(max_digits=19, decimal_places=10)

    @with_author 
    class UOM(models.Model):
        version = IntegerVersionField( )
        code = models.CharField(max_length=30)  
        name = models.CharField(max_length=30) 
        material =  models.ForeignKey(Material) 
        description = models.TextField(null=True, blank=True)



@with_author 
class UOM_BINUOM(models.Model):
    version = IntegerVersionField( )
    UOM =  models.ForeignKey(UOM) 
    BIN_UOM =  models.ForeignKey(BIN_UOM) 
    quantityperunit = models.PositiveSmallIntegerField()
    creation_time = models.DateTimeField(auto_now_add=True,blank=True)

在输入中我有我的材料 ID

使用 django ORM 我想获取我的材料 ID 的 UOM_BINUOM 的所有对象。

在 SQL 中:

 Select * from UOM_BINUOM where UOM_BINUOM.uom in (Select uom from UOM where UOM.material = material)

在 ORM 中,我正在尝试这样:

uombinuom = UOM.objects.filter(material__in=material_id).uom_binuom_set.all().order_by('-id')

或者像这样

    material = get_object_or_404(Material, pk=material_id)
    uom = material.uom_set.all().order_by('-id')
    uombinuom = uom.uom_binuom_set.all().order_by('-id')

但出现错误

'QuerySet' 对象没有属性 'uom_binuom_set'

我做错了什么,我该如何解决这个问题?

【问题讨论】:

  • material_id 是 id 列表,还是单个 id?
  • material_id 是单个 id

标签: django orm


【解决方案1】:

uom_binuom_set 用于单个 UOM 实例

UOM.objects.get(pk=1).uom_binuom_set.all()

但是,您有UOM_BINUOM.objects.filter(...),它是一个查询集。正如错误所说,查询集没有 uom_binuom_set 方法。

您可以从UOM_BINUOM 模型开始构建您想要的查询集。

uombinuom = UOM_BINUOM.objects.filter(UOM__material=material_id)

请注意,由于material_id 是单个id,因此您不需要使用__in

【讨论】:

    【解决方案2】:

    当你这样做时

    uom = material.uom_set.all().order_by('-id')
    

    uom 在这里是QuerySetuom_binuom_set 应该在单个记录而不是 QuerySet 记录上调用。 因此,您需要遍历 uom QuerySet 并为每条记录调用 .uom_binuom_set.all()

    for record in uom:
       uom_binuom =  record.uom_binuom_set.all()
       # do something with uom_binuom
    

    或者,如果您只想要与uom_binuom 相关的第一条记录,那么

    uom_binuom = uom.first().uom_binuom_set.all()
    

    【讨论】:

      猜你喜欢
      • 2020-10-31
      • 1970-01-01
      • 2021-08-05
      • 2017-06-22
      • 2013-05-10
      • 1970-01-01
      • 2011-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多