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


Reply via email to