I have created an app here people can upload 'csv' files of their items details to the server, and then perform various opertaions upon it. But, because every item has it's own unique ID, whenever a csv is uploaded, there is no way to figure out which item-batch was uploaded last.
Upload function
This is my function in views.py importing the data in the Itembatch model:
@method_decorator([login_required, teacher_required], name='dispatch')
class UploadedItems(ListView):
model = ItemBatch
ordering = ('name',)
context_object_name = 'quizzes'
template_name = 'classroom/teachers/item_list.html'
def get_queryset (self):
# queryset = self.request.user.uploaded_by
print("----------------" + str(ItemBatch.objects.latest('time')))
return ItemBatch.objects.filter(uploaded_by=self.request.user)
And this is the model:
# item upload
class ItemBatch(models.Model):
# uploaded_by = models.ForeignKey(Teacher, on_delete=models.CASCADE, related_name='uploaded_by')
ttypes =(('Open','Open'),('Container','Container'),('Trailer','Trailer'),('All','All'))
uploaded_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name='uploaded_by')
name = models.CharField(max_length=30)
pid = models.CharField(max_length=30)
quantity = models.CharField(max_length=30)
length = models.CharField(max_length=100, blank=True)
width = models.CharField(max_length=100, blank=True)
height = models.CharField(max_length=100, blank=True)
volume = models.CharField(max_length=100, blank=True)
weight = models.CharField(max_length=100, blank=True)
truck_type = models.CharField(max_length=255,default=0, choices=ttypes)
origin = models.CharField(max_length=100, blank=True)
destination = models.CharField(max_length=100, blank=True)
time = models.DateTimeField(max_length=100, blank=True,default=now)
def __str__ (self):
return self.name
What I tried
As you can see,
print("----------------" + str(ItemBatch.objects.latest('time')))
prints only the latest item and not the whole batch which I need. I need a queryset with all the items which were has the same time field and is the latest. Is there any way to do this without using any kind of BatchID in the model ?
This is my models database:
As you can see, I want to segregate/differentiate between different batches of items.