У меня есть счет инвентаря, в котором есть N местоположений, это место нужно считать N раз, поэтому у меня есть одна модель для «заголовка местоположения» и другая для списка элементов каждого заголовка.Django/Python Цепочки и сортировка Querysets
Мне нужно цепи, сортировать и получить уникальные результаты элементов в N querysets
У меня есть это:
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
и не повторяются
Я застрял здесь, я не знаю, правильно ли цепь делает это правильно, и функция sort дает мне ошибку, говоря, что элемент не имеет атрибута «epc».
Справка plz!
Можете ли вы добавить свои модели plz? – cdvv7788