1

I have 3 models Material, 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)

On the input I have my material ID

Using django ORM I want to get all the objects of UOM_BINUOM of my material id.

In SQL :

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

In ORM I am trying to it like this:

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

or like this

    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')

but getting error

'QuerySet' object has no attribute 'uom_binuom_set'

What I am doing wrong and how can I solve this issue?

2
  • Is material_id a list of ids, or a single id? Commented May 16, 2016 at 16:46
  • material_id is single id Commented May 16, 2016 at 16:47

2 Answers 2

1

The uom_binuom_set is for a single UOM instance

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

However, you have UOM_BINUOM.objects.filter(...), which is a queryset. As the error says, the queryset does not have the uom_binuom_set method.

You can construct the queryset you want by starting with the UOM_BINUOM model.

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

Note that since material_id is a single id, you don't need to use __in.

Sign up to request clarification or add additional context in comments.

Comments

1

When you do

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

uom is a QuerySet here. uom_binuom_set should be called on single record not QuerySet of records. So you will need to iterate over the uom QuerySet and call .uom_binuom_set.all() for each record.

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

Or if you want only first records related uom_binuom then

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.