Django

Code

Show
Ignore:
Timestamp:
08/01/08 10:54:53 (4 months ago)
Author:
lukeplant
Message:

Fixed #2175: Added tests for models with multiple GenericForeignKeys?

Also fixed small typo in a docstring.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/tests/modeltests/generic_relations/models.py

    r6900 r8170  
    2828        return self.tag 
    2929 
     30class 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 
    3048class Animal(models.Model): 
    3149    common_name = models.CharField(max_length=150) 
     
    3351 
    3452    tags = generic.GenericRelation(TaggedItem) 
     53    comparisons = generic.GenericRelation(Comparison,  
     54                                          object_id_field="object_id1", 
     55                                          content_type_field="content_type1") 
    3556 
    3657    def __unicode__(self): 
     
    137158[<Animal: Platypus>] 
    138159 
     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>] 
    139194"""}