Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Query the first object of all user related objects in Django

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading