Ticket #8076: patch_for_getnext_with_subclasstests.diff

File patch_for_getnext_with_subclasstests.diff, 2.9 KB (added by jan_oberst, 16 years ago)

Added tests for get_next/previous_FIELD / fixed by adding an additional check if the model is a subclass

  • django/db/models/base.py

     
    438438        op = is_next and 'gt' or 'lt'
    439439        order = not is_next and '-' or ''
    440440        param = smart_str(getattr(self, field.attname))
     441        # Hackish, but add another __pk lookup if the model is a child model
     442        pk_string = self.__class__._meta.parents and 'pk__pk__' or 'pk__'
    441443        q = Q(**{'%s__%s' % (field.name, op): param})
    442         q = q|Q(**{field.name: param, 'pk__%s' % op: self.pk})
     444        q = q|Q(**{field.name: param, '%s%s' % (pk_string,op): self.pk})
    443445        qs = self.__class__._default_manager.filter(**kwargs).filter(q).order_by('%s%s' % (order, field.name), '%spk' % order)
    444446        try:
    445447            return qs[0]
  • tests/modeltests/lookup/models.py

     
    1616    def __unicode__(self):
    1717        return self.headline
    1818
     19class ParentModel(models.Model):
     20    headline = models.CharField(max_length=100)
     21    pub_date = models.DateTimeField()
     22    def __unicode__(self):
     23        return self.headline
     24
     25class ChildModel(ParentModel):
     26    stupid = models.CharField(max_length=100)
     27
    1928__test__ = {'API_TESTS':r"""
    2029# Create a couple of Articles.
    2130>>> from datetime import datetime
     
    3443>>> a7 = Article(headline='Article 7', pub_date=datetime(2005, 7, 27))
    3544>>> a7.save()
    3645
     46# Create some childclass objects
     47>>> c1 = ChildModel(headline='ArticleChild 1', pub_date=datetime(2005, 8, 1, 3, 0))
     48>>> c1.save()
     49>>> c2 = ChildModel(headline='ArticleChild 2', pub_date=datetime(2005, 8, 1, 10, 0))
     50>>> c2.save()
     51>>> c3 = ChildModel(headline='ArticleChild 3', pub_date=datetime(2005, 8, 2))
     52>>> c3.save()
     53
    3754# text matching tests for PostgreSQL 8.3
    3855>>> Article.objects.filter(id__iexact='1')
    3956[<Article: Article 1>]
     
    234251>>> a2.get_previous_by_pub_date()
    235252<Article: Article 1>
    236253
     254# test subclassed models for get previous/next, too
     255>>> c1.get_next_by_pub_date()
     256<ChildModel: ArticleChild 2>
     257>>> c2.get_next_by_pub_date()
     258<ChildModel: ArticleChild 3>
     259>>> c3.get_next_by_pub_date()
     260Traceback (most recent call last):
     261    ...
     262DoesNotExist: ChildModel matching query does not exist.
     263>>> c3.get_previous_by_pub_date()
     264<ChildModel: ArticleChild 2>
     265>>> c2.get_previous_by_pub_date()
     266<ChildModel: ArticleChild 1>
     267>>> c1.get_previous_by_pub_date()
     268Traceback (most recent call last):
     269    ...
     270DoesNotExist: ChildModel matching query does not exist.
     271
    237272# Underscores and percent signs have special meaning in the underlying
    238273# SQL code, but Django handles the quoting of them automatically.
    239274>>> a8 = Article(headline='Article_ with underscore', pub_date=datetime(2005, 11, 20))
Back to Top