Add is_equal method to models for other kinds of equality checking.
|Reported by:||Michael Axiak <axiak@…>||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||master|
|Cc:||Michael, Axiak, <axiak@…>, ferringb@…||Triage Stage:||Design decision needed|
|Has patch:||yes||Needs documentation:||yes|
|Needs tests:||yes||Patch needs improvement:||no|
There are many cases where one might want to check to see if two models are equal (and not just the same database row). If you take the OO analogy, you might want to see if two Articles are equivalent articles even if they are not the 'same' object.
This patch will allow one to do that. You can specify which fields you want to exclude by specifying attnames in the exclude parameter. If it's left unspecified, it will automatically exclude the primary key field(s). To disable excluding the primary key, just set excludes to .
class Article(models.Model): subject = models.CharField(maxlength=512) submitted = models.DateTimeField() author = models.ForeignKey(User) # suppose a and b only differ on when they were submitted a = Article.objects.get(id = 5) b = Article.objects.get(id = 6) a.is_equal(b) False a.is_equal(a) True a.is_equal(b, ['id','submitted']) True a.is_equal(b, ) False a.is_equal(a, ) True
Change History (8)
comment:1 Changed 9 years ago by Michael Axiak <axiak@…>
- Needs documentation set
- Needs tests set
- Patch needs improvement unset
- Triage Stage changed from Unreviewed to Design decision needed
comment:4 in reply to: ↑ 3 Changed 9 years ago by (removed)
- Cc Michael Axiak <axiak@…> ferringb@… added; Michael Axiak <axiak@…> removed