Home > Mobile >  Query the first object of all user related objects in Django
Query the first object of all user related objects in Django

Time:10-18

I have a django model like below:

class Books:
    name = models.CharField(max_length=255)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    timeStamp = models.DateTimeField(auto_now_add=True)
    first = models.BooleanField(default=False)

I already have like a million entries in the Books table and added first field recently. Now I want to update the first field of the first created object of every user to True.

all = Books.objects.all()
li = []
for i in all:
   if i.user.username not in li:
       i.first = True
       i.save()
       li.append(i.user.username)

I tried the following function but it takes so long to execute, with much data in it. So is there a faster way? Tried the bulk_update method, for that also the loop should be completed.

CodePudding user response:

Because you are using PostgreSQL you can call .distinct() on specific column, and after that use queryset .update() method to update all columns to same value which should be a lof faster.

Books.objects.all().order_by('timeStamp').distinct('author').update(first=True) 

Code above first orders queryset by timeStamp field, after that calling distinct on author column leaving only first distinct row (earliest) and in the end updates column value.

  • Related