Matching DateTime objects in test cases with Django Rest Framework

If your model has a date_created or modified etc. kind of object(s) probably you will face a datetime object mismatch issue while asserting equality of your dictionary object with the rendered response dictionary. So, I have created a small function which will help you resolve it.

 

DATETIMEHELPER = lambda x : str(x.isoformat().split('.')[0])+'Z'

 

Now, you can call your data object as :

data = {...,"date_created":DATETIMEHELPER(self.model_name.date_created),...}
response.render()
self.AssertEqual(json.dumps(response.content), data)