Thursday, April 18, 2013

Re: How to check for a specific field change in a model?

There is actually, via a third party app that makes it all dead
simple, it's called django-model-utils and is available here:

https://github.com/carljm/django-model-utils

Credit where it's due, I discovered this wonderful module via PyDanny
and Audrey's Two Scoops book.

Cheers
L.

On 19 April 2013 09:15, akaariai <akaariai@gmail.com> wrote:
> On 18 huhti, 20:34, deepankar bajpeyi <dbajp...@gmail.com> wrote:
>> I have my models.py :
>>
>> class Hotel(models.Model):
>> name = models.CharField(max_length=20)
>> currency = models.ForeignKey(Currency)
>>
>> class Currency(models.Mode):
>> code = models.CharField(max_length=3)
>> name = models.CharField(max_length=10)
>>
>> Whenever the currency field in hotel is changing I need to be able to do
>> something. So I have a function like :
>>
>> @receiver(pre_save,sender=Hotel)def update_something(sender,**kwargs)
>> obj = kwargs['instance']
>> old_object = Hotel.objects.get(pk=obj.pk)
>> '''
>> Now I can do anything here comparing the oldo object with the instance
>> '''
>>
>> The thing is I don't want to make a query for this, since then the purpose
>> of signals becomes stupid and I become a fool.
>>
>> So I should be able to do something like :
>>
>> updated = kwargs['update_fields']
>> new_currency = updated['currency']
>>
>> Is their a way I can find out that change for *ONLY* one particular field
>> say currency ,instead of doing a query like this. I want to get the changes
>> related to the currency foreign key and update things before I save.
>>
>> Thanks :)
>
> Unfortunately there isn't any good way to do this, just doing an extra
> query on save seems like the best way forward.
>
> Django doesn't currently store the DB fetched values anywhere. The
> reason for this is that doing so is somewhat expensive. It would be
> nice to offer doing this as an option, this would allow for some nice
> features, like automatic update of only changed fields, and updatable
> natural primary keys.
>
> - Anssi
>
> --
> 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+unsubscribe@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?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
The new creativity is pointing, not making. Likewise, in the future,
the best writers will be the best information managers.

http://www.theawl.com/2013/02/an-interview-with-avant-garde-poet-kenneth-goldsmith

--
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+unsubscribe@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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate