Reputation: 26538
Lets say I have a CustomerProfile
model
class CustomerProfile(models.Model):
billing_profile = models.ForeignKey(...)
and when I run the assertEqual
on 2 unsaved objects, it raises AssertionError
self.assertEqual(
CustomerProfile(billing_profile=default_profile),
CustomerProfile(billing_profile=default_profile)
)
Gives the following error:
AssertionError: <CustomerProfile: Full Charge None> != <CustomerProfile: Full Charge None>
I don't understand why because the instance id would not have been populated since it's unsaved.
Upvotes: 1
Views: 513
Reputation: 362836
There is no special support in assertEqual
for comparing Django models.
Unless the models have been saved (i.e. have a primary key) then they are comparing by identity (CPython: memory location), which will always be different for two separate unsaved model instances, even if every field is the same.
To compare unsaved model instances based on content, you would need to manually check that the data in all the relevant fields are equal. Third-party testfixtures
has a helper for this: see django_compare
.
Upvotes: 2