0

im new with this django database querying.

how can i translate this into a django queryset

SELECT prod.item_code_id, prod.name, price.SRP 
FROM inventory_product prod, inventory_pricing price 
WHERE prod.item_code_id = price.item_code_id

class Product(models.Model):
  item_code_id = models.AutoField(primary_key=True)
  name = models.CharField(max_length=255)
  description = models.CharField(max_length=255)
  category = models.ForeignKey(Category)
  color = models.ForeignKey(Color)
  brand = models.ForeignKey(Brand)
  item_size = models.ForeignKey(ItemSize)

class Pricing(models.Model):
  product_id = models.AutoField(primary_key=True)
  item_code = models.ForeignKey(Product)
  supplier = models.ForeignKey(Supplier)
  SRP = models.DecimalField(max_digits=5, decimal_places=2)
kinoki
  • 13
  • 4
  • You need to perform a JOIN which is covered [here][1]. [1]: http://stackoverflow.com/questions/4125379/django-implementing-join-using-django-orm – Neil Trodden May 28 '14 at 07:48
  • Or you can make raw querys: https://docs.djangoproject.com/en/dev/topics/db/sql/#mapping-query-fields-to-model-fields – cor May 28 '14 at 07:51
  • how can I make the "WHERE prod.item_code_id = price.item_code_id" work? because the link is for getting a specific item. – kinoki May 28 '14 at 07:53
  • It is a mistake to approach Django with an SQL mindset. You have models: you should think about how to get the output you want using those models, rather than starting with an SQL query. Since you haven't posted them, though, we can't help you with that. – Daniel Roseman May 28 '14 at 08:41
  • Please post your Models without that we won't be able to help you. – Silwest May 28 '14 at 08:47
  • updated with models, thanks. – kinoki May 28 '14 at 09:13

1 Answers1

0

Daniel Roseman is right. Don't use SQL query unless you really need them.

In your example, Django ORM should be enough. It would be better if you posted you models, but it will look smth like:

for prod in Product.objects.all():
  price = Pricing.objects.get(item_code=prod)
  print prod.pk, prod.name, price.SRP
olzhas
  • 26
  • 5