tags:

views:

20

answers:

1

Where am i goofing in the following lookup?

My models:

Class Item(models.Model):
    item_code = models.CharField(max_length=10)
    name      = models.CharField(max_length=255)
    ....

Class Stock(models.Model):
    item_code = models.ForeignKey( Item )
    qty       = models.IntegerField()
    ...

Now i want to get all Stock objects with Item.item_code

stock_items = Stock.objects.filter(item__item_code__contains='101')

I get the error:

FieldError: Cannot resolve keyword 'item' into field. Choices are: id, item_code, qty, restock_level, userid 

What am i missing?

Gath

+3  A: 

You have to use following:

stock_items = Stock.objects.filter(item_code__item_code__contains='101')

First field name is field in Stock model, in this case item_code, not item.

In order to use in way you did. You would have to change your Stock model to following:

Class Stock(models.Model):
    item = models.ForeignKey(Item)
    qty = models.IntegerField()

Then this would work:

stock_items = Stock.objects.filter(item__item_code__contains='101')
Dominik Szopa
Thanks, its confusing especially if you follow the Django documentation on this; http://docs.djangoproject.com/en/dev/topics/db/queries/
gath
Agreed, totally.
gath