开发者

How to span lookup relationship in django?

开发者 https://www.devze.com 2023-01-18 01:07 出处:网络
Where am i goofing in the following lookup? My models: Class Item(mo开发者_开发知识库dels.Model):

Where am i goofing in the following lookup?

My models:

Class Item(mo开发者_开发知识库dels.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


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

精彩评论

暂无评论...
验证码 换一张
取 消