Get very last object of database

23 views
Skip to first unread message

Stéphane Manguette

unread,
Jan 30, 2019, 5:02:02 PM1/30/19
to Django users
Hello guys, 

My issue is pretty simple. I want to get the very last record of my database (which is populated with thousands of entries)

So far ->very easy. But I want to make sure that the access to the database is as light as possible. Don't want django to parse everything in my database.

Kindly note that my DB has Primary key field.

I tried 
Mydb.objects.all()[:1]

Mydb.objects.all()[1:]



but both return the first filed instead of the last one.

Thanks for your help

Stephane

Matthew Pava

unread,
Jan 30, 2019, 5:08:08 PM1/30/19
to django...@googlegroups.com

You can try Mydb.objects.last()

You can also try reversing the order and using first:  Mydb.objects.order_by(‘-pk’).first()

 

And for some terminology clarification, a database consists of tables.  In Django, a model maps to a table, not a database.  A table has fields; a database has tables but not fields.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/32533a94-d27f-4091-9910-6644cd11ea49%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stéphane Manguette

unread,
Jan 30, 2019, 5:11:30 PM1/30/19
to Django users
Hi Matthew,

Thanks for your prompt answer; I will keep it in mind for terminology, totally get it, Thanks !

Stéphane


Le mercredi 30 janvier 2019 23:08:08 UTC+1, Matthew Pava a écrit :

You can try Mydb.objects.last()

You can also try reversing the order and using first:  Mydb.objects.order_by(‘-pk’).first()

 

And for some terminology clarification, a database consists of tables.  In Django, a model maps to a table, not a database.  A table has fields; a database has tables but not fields.

 

From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of Stéphane Manguette
Sent: Wednesday, January 30, 2019 4:02 PM
To: Django users
Subject: Get very last object of database

 

Hello guys, 

 

My issue is pretty simple. I want to get the very last record of my database (which is populated with thousands of entries)

 

So far ->very easy. But I want to make sure that the access to the database is as light as possible. Don't want django to parse everything in my database.

 

Kindly note that my DB has Primary key field.

 

I tried 

Mydb.objects.all()[:1]

Mydb.objects.all()[1:]

 

 

but both return the first filed instead of the last one.

 

Thanks for your help

 

Stephane

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to djang...@googlegroups.com.

Odile Lambert

unread,
Jan 30, 2019, 5:25:30 PM1/30/19
to django...@googlegroups.com

Hello

I have a field in my model which has unique = True.

If I specify an error message with a reference to the value of the field, Django crashes with Key error.

Yet this works for other error_messages. How wan I solve this?

Here is the code and the trace back

https://dpaste.de/t5X8#

Is this a bug in admin?

I thank you in advance for your help.
Piscvau





Reply all
Reply to author
Forward
0 new messages