I want to retrieve the latest object created for a particular user.
Let’s say I have a model:
class LogBookPreTransaction(models.Model):
person = models.ForeignKey(User)
code = models.CharField(max_length=20)
address = models.CharField(max_length=200,null =True)
pincode = models.CharField(max_length=20,null =True)
Now I want to retrieve the latest object created for a user = “X”.
How can we do that?
transaction_obj = LogBookPreTransaction.objects.filter(user = "x").latest()
Off course this won’t work, but I want something like this. Is the question clear?
You can use latest only if you have a date field, which you have so try:
Try also this, it can be useful when you don’t have a date field: