How to latest objects created in django models by

2019-08-17 14:41发布

问题:

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.

回答1:

For what i could understand from your question, you maybe need this (withouth testing it). Disable and enable USE_TZ if its True in settings.py.

latest_item = ItemBatch.objects.latest('time') 
from django.conf import settings
settings.USE_TZ = False 
latest_items = ItemBatch.objects.filter(time__date=latest_item.time.date(), time__hour=latest_item.time.hour, time__minute=latest_item.time.minute) 
settings.USE_TZ = True

'latest_items' contains all elements that match the date, hour and minute as the latest ItemBatch object. Add second too if you want it, but it may not work if there are lots of elements added in the same time.