.save() is not working for .first()

my_query = Model.objects.using(‘dbname’).filter(ref_id=my_ref_id)
my_query_first = my_query.first()
my_query_first.last_updated = date_obj1.replace(tzinfo=tzlocal()) - timedelta(days=1)


I couldn’t able to update the value of first in queryset, after saving it and if I print the first values means it doesn’t changing with older values

Welcome @dinesh1299 !

What does your Model look like?

Is this code part of a view, or are they commands you’re entering in the shell? (Or running somewhere else - if so, where and how?)

What is date_obj1?

Do you get any errors in the console where this is being run?

My model will look like this
class Model(models.Model):
ref_id= models.IntegerField(blank=True, null=True)
results = models.JSONField(null=True, default=dict, blank=True)
last_updated = models.DateTimeField(auto_now=True, null=True)

and date_obj1 is
date_obj1 = datetime.strptime(request.GET.get(‘date_updated’), ‘%m-%d-%Y’)

my_query = Model.objects.using(‘dbname’).filter(ref_id=my_ref_id) → this queryset will return multiple i need to update the first occurance of this query, when I tried to update the field by using the method I’ve posted in this question it’s not get updating

You’re trying to manually update a field with auto_now=True.

Quoting from the docs for auto_now:

Note that the current date is always used; it’s not just a default value that you can override.

You can’t set that to any arbitrary value.

1 Like