1

I need to check my object related or not ORM postgresql/

I have two object ItemVariationValue and ItemVariation i need to check ItemVariationValue has relation with ItemVariation

models.py

class ItemVariation(models.Model):
    item=models.ForeignKey(Item,on_delete=models.CASCADE)
    price=models.IntegerField(blank=True,null=True,default=0)
    item_code=models.CharField(max_length=500)
    keywords= models.ManyToManyField(Keyword)
    image=models.ImageField(upload_to='dishes/', blank=True, null=True)
    def __str__(self):
        return str(self.id)

class ItemVariationValue(models.Model):
    item=models.ForeignKey(Item,on_delete=models.CASCADE)
    item_variation=models.ForeignKey(ItemVariation,on_delete=models.CASCADE)
    attribute=models.ForeignKey(Attribute,on_delete=models.CASCADE)
    attribute_value=models.ForeignKey(AttributeValue,on_delete=models.CASCADE)
    def __str__(self):
        return str(self.id)

views.py

def get_items(request):    # request= {"order_details": "varxxx:1"}
    order=request.data['order_details']
    items = order.split(',')
    total = 0
    for item in items:
        od = item.split(':')
        sku = str(od[0])
        qty = int(od[1])
        itemvariation=ItemVariation.objects.get(item_code=sku)



# if ItemVariationValue.item_variation has ForeignKey(ItemVariation):
  • 3
    Please first fix the attribute names: `ForeignKey`s should *not* have an `_id` suffix. Right now this will introduce attributes like `item_variation_id_id`. – Willem Van Onsem Sep 20 '18 at 10:56

2 Answers2

1

Note (ForeignKey nomenclature): ForeignKeys should not have an _id suffix, since Django will automatically construct an extra field fieldname_id that contains the primary key. By writing an _id suffix, you will introduce extra attributes like item_variation_id_id. Although strictly speaking that is not a problem, it introduces a lot of confusion. For example my_itemvariationvalue.itemvariation_id will result in an ItemVariation object, etc.

If you fix the ForeignKey names, you can check this like:

# given the ForeignKey is named itemvariation, not itemvariation_id and use Comparison Operators 

if my_itemvariationvalue.itemvariation_id == my_itemvariation.id:
    # ... (objects are related)
else:
    # ... (otherwise)

By using the itemvariation_id here, we avoid loading the related object, and thus potentially an extra database query.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • why are you using my_ is this suffix ? –  Sep 20 '18 at 11:18
  • 1
    @M4STER: the `my_itemvariationvalue` is the `ItemVariationObject`, furthermore that is a prefix :) – Willem Van Onsem Sep 20 '18 at 11:18
  • It is not clear can you make it clear do i have do declare my_itemvariationvalue ? –  Sep 21 '18 at 13:32
  • @M4STER: In your question you write "*I have two object ItemVariationValue and ItemVariation*", well if these objects are named `my_intemvariationvalue` and `my_itemvariation` then it does the trick. Somehow you need a reference to your objects. – Willem Van Onsem Sep 21 '18 at 13:34
  • i have added view. can make me clear with this example ? –  Sep 21 '18 at 13:48
  • 1
    But the above does not make much sense, since an `ItemVariation` can have zero, one, or *more* `ItemVariationValue`s. A `ForeignKey` is after all a many-to-one relation. Furthermore after sevaeral edits, you still fail to make the problem statement clear. What exactly do you *want* to do, not *how* do you want to do it? – Willem Van Onsem Sep 21 '18 at 13:50
  • I need to find single product and variable product and i have followed this answer https://stackoverflow.com/questions/24923469/modeling-product-variants –  Sep 21 '18 at 14:23
  • @M4STER: but the point is that it is *not* guaranteed that there is *one* `ItemVariationValue`, there can be *no* related `ItemVariationValue`, there can be *one*, and there can be mulitple. So it looks like you modeled it the wrong way. If a`ItemVariation` has (at most) one `ItemVariationValue`, you need to use a `OneToOneField`. Do you understand what kind of relation a `ForeignKey` aims to model? A many-to-one field, so that means that *many* `ItemVariationValue`s can map to the *same* `ItemVariation` object. – Willem Van Onsem Sep 21 '18 at 14:31
0

As far as i know you have a ItemVariationValue object and you want to know if this object has ItemVariation. so you can easily do

#your code
itemvariation=ItemVariation.objects.get(item_code=sku)

check_relation_obj=itemvariation.item_variation

also if you think you may or may not have relation with ItemVariation add blank=True,null=True with Foreign Key.

Dhnesh Dhingra
  • 301
  • 1
  • 7