> Den 25/03/2015 kl. 20.20 skrev felix <fe...@epepm.cupet.cu>:
> 
> Yes I know I can't acces the Manager from a model instance, but I need to 
> check the value of a field saved in the database before updating it. Can I do 
> it from the Model clean() method?

Depending on your requirements, there's a couple of options.

Use refresh_from_db() in up-coming Django 1.8 
(https://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.refresh_from_db)
or the poor man's version:

   new_obj = MyModel.objects.get(pk=obj.pk)

Remember to audit your use of transactions.

If you always want to update a value, e.g. increment a counter, you can use an 
F() expression: 
https://docs.djangoproject.com/en/1.7/ref/models/queries/#f-expressions

If none of these are appropriate, you need to describe your requirements in 
more detail.


Erik

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/29A8DA8B-9C59-4742-8172-EC119EFD2426%40cederstrand.dk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to