Ticket #8076: patch_for_getnext_with_subclasstests.diff
File patch_for_getnext_with_subclasstests.diff, 2.9 KB (added by , 16 years ago) |
---|
-
django/db/models/base.py
438 438 op = is_next and 'gt' or 'lt' 439 439 order = not is_next and '-' or '' 440 440 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__' 441 443 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}) 443 445 qs = self.__class__._default_manager.filter(**kwargs).filter(q).order_by('%s%s' % (order, field.name), '%spk' % order) 444 446 try: 445 447 return qs[0] -
tests/modeltests/lookup/models.py
16 16 def __unicode__(self): 17 17 return self.headline 18 18 19 class ParentModel(models.Model): 20 headline = models.CharField(max_length=100) 21 pub_date = models.DateTimeField() 22 def __unicode__(self): 23 return self.headline 24 25 class ChildModel(ParentModel): 26 stupid = models.CharField(max_length=100) 27 19 28 __test__ = {'API_TESTS':r""" 20 29 # Create a couple of Articles. 21 30 >>> from datetime import datetime … … 34 43 >>> a7 = Article(headline='Article 7', pub_date=datetime(2005, 7, 27)) 35 44 >>> a7.save() 36 45 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 37 54 # text matching tests for PostgreSQL 8.3 38 55 >>> Article.objects.filter(id__iexact='1') 39 56 [<Article: Article 1>] … … 234 251 >>> a2.get_previous_by_pub_date() 235 252 <Article: Article 1> 236 253 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() 260 Traceback (most recent call last): 261 ... 262 DoesNotExist: 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() 268 Traceback (most recent call last): 269 ... 270 DoesNotExist: ChildModel matching query does not exist. 271 237 272 # Underscores and percent signs have special meaning in the underlying 238 273 # SQL code, but Django handles the quoting of them automatically. 239 274 >>> a8 = Article(headline='Article_ with underscore', pub_date=datetime(2005, 11, 20))