Thursday, August 5, 2010

Re: overriding model.save()

The "new" values are what you just set: in your example, self.a=3 and self.b=4 if you're inside your custom save method. Then you can get the current values from the database from inside your custom save with something like:

current = Foo.objects.get(pk=self.pk)

and inspect current.b for special values.

On Thu, Aug 5, 2010 at 2:54 PM, Sells, Fred <fred.sells@adventistcare.org> wrote:
That part makes sense, but where would I find the "new" values that have been set but not saved when my custom save() method is called? Like

X.a=3
X.b=4
X.save()

I want to see if b is a special value before saving.

-----Original Message-----
From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On Behalf Of Sam Lai
Sent: Thursday, August 05, 2010 7:58 AM
To: django-users@googlegroups.com
Subject: Re: overriding model.save()

On 5 August 2010 03:05, Sells, Fred <fred.sells@adventistcare.org> wrote:
> I would like to prevent saving a new value if the database contains a
> specific value.  This is on a per field, per record basis.
>
> If I override the save() method; is there a way to find the existing (in
> the DB) values and the new (to be stored) values?

Just perform database queries as per normal inside the save() method.

You have access to the object to be stored as well, see
http://docs.djangoproject.com/en/dev/topics/db/models/#overriding-model-methods

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment