Django

Code

Changeset 8814

Show
Ignore:
Timestamp:
09/01/08 16:04:01 (3 months ago)
Author:
jacob
Message:

Fixed #8076: fixed get_(next/previous)_by_date when used with subclasses. Thanks, bjornkri and jan_oberst.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/fields/related.py

    r8782 r8814  
    143143            sql, params = value.as_sql() 
    144144            return QueryWrapper(('(%s)' % sql), params) 
    145         if lookup_type == 'exact': 
     145 
     146        # FIXME: lt and gt are explicitally allowed to make 
     147        # get_(next/prev)_by_date work; other lookups are not allowed since that 
     148        # gets messy pretty quick. This is a good candidate for some refactoring 
     149        # in the future. 
     150        if lookup_type in ['exact', 'gt', 'lt']: 
    146151            return [pk_trace(value)] 
    147152        if lookup_type in ('range', 'in'): 
  • django/trunk/tests/regressiontests/model_inheritance_regress/models.py

    r8128 r8814  
    5959class SelfRefChild(SelfRefParent): 
    6060    child_data = models.IntegerField() 
     61 
     62class Article(models.Model): 
     63    headline = models.CharField(max_length=100) 
     64    pub_date = models.DateTimeField() 
     65    class Meta: 
     66        ordering = ('-pub_date', 'headline') 
     67 
     68    def __unicode__(self): 
     69        return self.headline 
     70 
     71class ArticleWithAuthor(Article): 
     72    author = models.CharField(max_length=100)  
    6173 
    6274__test__ = {'API_TESTS':""" 
     
    195207>>> obj.delete() 
    196208 
     209# Regression tests for #8076 - get_(next/previous)_by_date should  
     210>>> c1 = ArticleWithAuthor(headline='ArticleWithAuthor 1', author="Person 1", pub_date=datetime.datetime(2005, 8, 1, 3, 0)) 
     211>>> c1.save() 
     212>>> c2 = ArticleWithAuthor(headline='ArticleWithAuthor 2', author="Person 2", pub_date=datetime.datetime(2005, 8, 1, 10, 0)) 
     213>>> c2.save() 
     214>>> c3 = ArticleWithAuthor(headline='ArticleWithAuthor 3', author="Person 3", pub_date=datetime.datetime(2005, 8, 2)) 
     215>>> c3.save() 
     216 
     217>>> c1.get_next_by_pub_date() 
     218<ArticleWithAuthor: ArticleWithAuthor 2> 
     219>>> c2.get_next_by_pub_date() 
     220<ArticleWithAuthor: ArticleWithAuthor 3> 
     221>>> c3.get_next_by_pub_date() 
     222Traceback (most recent call last): 
     223    ... 
     224DoesNotExist: ArticleWithAuthor matching query does not exist. 
     225>>> c3.get_previous_by_pub_date() 
     226<ArticleWithAuthor: ArticleWithAuthor 2> 
     227>>> c2.get_previous_by_pub_date() 
     228<ArticleWithAuthor: ArticleWithAuthor 1> 
     229>>> c1.get_previous_by_pub_date() 
     230Traceback (most recent call last): 
     231    ... 
     232DoesNotExist: ArticleWithAuthor matching query does not exist. 
     233 
    197234"""}