lundi 19 janvier 2015

Is there (already) a way to compare 2 model instances, field for field, to see if they are equal?

I'm trying to make an assertion inside one of my tests that the fields from a model have not changed. I know that philosophically this is incorrect, but since i control all the variables i need to know about, i just want to check that my DB entry hasn't changed.


I am willing to accept a solution that can transform this into an assertion that some API wasn't called, which is supposed to update models, but i am aware that the API might not be fully documented, so i'd prefer it there was a way to just check if all the field values are equal.


Anyway, i know how to do this, but i'm using django 1.6 and the Model._meta API isn't public yet:



def assertFieldsEqual(self, instance1, instance2)
for field_name in MyModel._meta.get_all_field_names():
if getattr(instance1, field_name) != getattr(instance2, field_name):
return False # or raise assertion error, or whatever
return True # or don't do nothing, or whatever


So is there a better way?


Aucun commentaire:

Enregistrer un commentaire