| | 30 | class Comparison(models.Model): |
|---|
| | 31 | """ |
|---|
| | 32 | A model that tests having multiple GenericForeignKeys |
|---|
| | 33 | """ |
|---|
| | 34 | comparative = models.CharField(max_length=50) |
|---|
| | 35 | |
|---|
| | 36 | content_type1 = models.ForeignKey(ContentType, related_name="comparative1_set") |
|---|
| | 37 | object_id1 = models.PositiveIntegerField() |
|---|
| | 38 | |
|---|
| | 39 | content_type2 = models.ForeignKey(ContentType, related_name="comparative2_set") |
|---|
| | 40 | object_id2 = models.PositiveIntegerField() |
|---|
| | 41 | |
|---|
| | 42 | first_obj = generic.GenericForeignKey(ct_field="content_type1", fk_field="object_id1") |
|---|
| | 43 | other_obj = generic.GenericForeignKey(ct_field="content_type2", fk_field="object_id2") |
|---|
| | 44 | |
|---|
| | 45 | def __unicode__(self): |
|---|
| | 46 | return u"%s is %s than %s" % (self.first_obj, self.comparative, self.other_obj) |
|---|
| | 47 | |
|---|
| | 160 | # Simple tests for multiple GenericForeignKeys |
|---|
| | 161 | # only uses one model, since the above tests should be sufficient. |
|---|
| | 162 | >>> tiger, cheetah, bear = Animal(common_name="tiger"), Animal(common_name="cheetah"), Animal(common_name="bear") |
|---|
| | 163 | >>> for o in [tiger, cheetah, bear]: o.save() |
|---|
| | 164 | |
|---|
| | 165 | # Create directly |
|---|
| | 166 | >>> Comparison(first_obj=cheetah, other_obj=tiger, comparative="faster").save() |
|---|
| | 167 | >>> Comparison(first_obj=tiger, other_obj=cheetah, comparative="cooler").save() |
|---|
| | 168 | |
|---|
| | 169 | # Create using GenericRelation |
|---|
| | 170 | >>> tiger.comparisons.create(other_obj=bear, comparative="cooler") |
|---|
| | 171 | <Comparison: tiger is cooler than bear> |
|---|
| | 172 | >>> tiger.comparisons.create(other_obj=cheetah, comparative="stronger") |
|---|
| | 173 | <Comparison: tiger is stronger than cheetah> |
|---|
| | 174 | |
|---|
| | 175 | >>> cheetah.comparisons.all() |
|---|
| | 176 | [<Comparison: cheetah is faster than tiger>] |
|---|
| | 177 | |
|---|
| | 178 | # Filtering works |
|---|
| | 179 | >>> tiger.comparisons.filter(comparative="cooler") |
|---|
| | 180 | [<Comparison: tiger is cooler than cheetah>, <Comparison: tiger is cooler than bear>] |
|---|
| | 181 | |
|---|
| | 182 | # Filtering and deleting works |
|---|
| | 183 | >>> subjective = ["cooler"] |
|---|
| | 184 | >>> tiger.comparisons.filter(comparative__in=subjective).delete() |
|---|
| | 185 | >>> Comparison.objects.all() |
|---|
| | 186 | [<Comparison: cheetah is faster than tiger>, <Comparison: tiger is stronger than cheetah>] |
|---|
| | 187 | |
|---|
| | 188 | # If we delete cheetah, Comparisons with cheetah as 'first_obj' will be deleted |
|---|
| | 189 | # since Animal has an explicit GenericRelation to Comparison through first_obj. |
|---|
| | 190 | # Comparisons with cheetah as 'other_obj' will not be deleted. |
|---|
| | 191 | >>> cheetah.delete() |
|---|
| | 192 | >>> Comparison.objects.all() |
|---|
| | 193 | [<Comparison: tiger is stronger than None>] |
|---|