python - How to check for a specific field change in a model? -


i have 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 currency field in hotel changing need able something. have function :

@receiver(pre_save,sender=hotel) def update_something(sender,**kwargs)     obj = kwargs['instance']     old_object = hotel.objects.get(pk=obj.pk)     '''      can here comparing oldo  object instance     ''' 

the thing don't want make query this, since purpose of signals becomes stupid , become fool.

so should able :

updated = kwargs['update_fields'] new_currency = updated['currency'] 

is way can find out change only 1 particular field currency ,instead of doing query this. want changes related currency foreign key , update things before save.

sorry bad english , not being able use technical terms.

thanks :)

rather hacky solution save object state on initialization.

from django.forms.models import model_to_dict  class currency(models.mode):     code = models.charfield(max_length=3)     name = models.charfield(max_length=10)      def __init__(self):         super(currency, self).__init__()         self.__state = model_to_dict(self)      def updated(self):         new_state = model_to_dict(self)         return dict(set(self.__state.iteritems()) - set(new_state.iteritems())) 

method updated return difference between initial , new states.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -