| | 91 | def is_equal(self, other, excludes = None): |
| | 92 | """ |
| | 93 | Returns a boolean if the two instances of a model are equal. |
| | 94 | If ``excludes`` is specified, all fields with the names given in |
| | 95 | ``excludes`` will not be considered in the comparison. |
| | 96 | If ``excludes`` is not specified, it will default to be the |
| | 97 | primary key. |
| | 98 | """ |
| | 99 | if self is other: return True |
| | 100 | if type(self) != type(other): return False |
| | 101 | |
| | 102 | # TODO: Handle for multiple pk fields if that ever happens |
| | 103 | field_excludes = [] |
| | 104 | if excludes is not None: |
| | 105 | field_excludes += excludes |
| | 106 | if self._meta.equality_exclude is not None: |
| | 107 | field_excludes += self._meta.equality_exclude |
| | 108 | if excludes is None and self._meta.equality_exclude is None: |
| | 109 | field_excludes = [self._meta.pk.attname] |
| | 110 | |
| | 111 | field_names = [field.attname for field in self._meta.fields |
| | 112 | if field.attname not in field_excludes ] |
| | 113 | |
| | 114 | for field_name in field_names: |
| | 115 | if getattr(self, field_name) != getattr(other, field_name): |
| | 116 | return False |
| | 117 | return True |
| | 118 | |
| | 119 | |