我有一个有N个位置的库存盘点,这个位置需要计数N次,所以我有一个模型用于“位置标题”,另一个用于每个标题的项目列表。Django/Python链和排序查询集
我需要连锁,排序和获得的N项的唯一结果查询集
我有这样的:
loc_id = request.POST['loc_id'] # the Id of my location pivot
inv_location = InventoryLocations.objects.get(pk=loc_id) # get the location count pivot
inv_locations = InventoryLocations.objects.filter(location=inv_location.location,
inventory=inv_location.inventory) #get all related locations counts
# At this point i can have N inv_locations
count_items = [] # list of items in all inventory counts
for l in inv_locations:
items = InventoryDetails.objects.filter(inventory_location = l) # get items of every count
count_items.append(items)
# Now I have all the items counted in the counts_items array, I need to get from this a single
# list of items Ordered and not repeated
all_items = chain(count_items) <<< IS THIS CORRECT??
sorted_items = sorted(all_items,key=lambda item: item.epc) << THIS GIVE ME ERROR
unique_items = ???
我的车型有:
class InventoryCount(models.Model):
...nothing important
class InventoryLocation(models.Model):
inventory= models.ForeignKey(InventoryCount)
location= models.ForeignKey(Location)
...
class InventoryDetails(models.Model):
inventory_location= models.ForeignKey(InventoryLocations)
epc = models.CharField(max_length=25, null=True, blank=True)
item= models.ForeignKey(Item)
...
基本上,我需要所有物品清单中的所有物品清单数量按epc
排序,并且不重复
我被困在这里,我不知道链是否正确,排序功能给我一个错误,说该项目没有'epc'属性。
帮助PLZ!
你可以添加你的模型plz? – cdvv7788 2014-12-01 16:56:58