Ticket #8291: 8291_django_con11.diff

File 8291_django_con11.diff, 3.3 KB (added by Marc Egli, 13 years ago)
  • django/db/models/options.py

     
    277277        """
    278278        Returns the requested field by name. Raises FieldDoesNotExist on error.
    279279        """
     280        if name == 'pk': # replace special value "pk" with primary key attribute name
     281            attname = self.pk.attname
     282        else:
     283            attname = name
    280284        to_search = many_to_many and (self.fields + self.many_to_many) or self.fields
    281285        for f in to_search:
    282             if f.name == name:
     286            if f.name == attname:
    283287                return f
    284288        raise FieldDoesNotExist('%s has no field named %r' % (self.object_name, name))
    285289
  • django/core/management/validation.py

     
    284284                # this format would be nice, but it's a little fiddly).
    285285                if '__' in field_name:
    286286                    continue
     287                if field_name == 'pk':
     288                    continue
    287289                try:
    288290                    opts.get_field(field_name, many_to_many=False)
    289291                except models.FieldDoesNotExist:
  • tests/modeltests/ordering/tests.py

     
    33
    44from django.test import TestCase
    55
    6 from models import Article
     6from models import Article, ArticleWithPKReverseDefaultOrdering
    77
    88
    99class OrderingTests(TestCase):
     
    135135            ],
    136136            attrgetter("headline")
    137137        )
     138
     139        a1 = ArticleWithPKReverseDefaultOrdering.objects.create(
     140            pk=1, headline="Article 1", pub_date=datetime(2005, 7, 26)
     141        )
     142        a2 = ArticleWithPKReverseDefaultOrdering.objects.create(
     143            pk=2, headline="Article 2", pub_date=datetime(2005, 7, 27)
     144        )
     145        a3 = ArticleWithPKReverseDefaultOrdering.objects.create(
     146            pk=3, headline="Article 3", pub_date=datetime(2005, 7, 27)
     147        )
     148        a4 = ArticleWithPKReverseDefaultOrdering.objects.create(
     149            pk=4, headline="Article 4", pub_date=datetime(2005, 7, 28)
     150        )
     151       
     152        self.assertQuerysetEqual(
     153            ArticleWithPKReverseDefaultOrdering.objects.all(), [
     154                "Article 4",
     155                "Article 3",
     156                "Article 2",
     157                "Article 1",
     158            ],
     159            attrgetter("headline")
     160        )
     161       
     162 No newline at end of file
  • tests/modeltests/ordering/models.py

     
    2424
    2525    def __unicode__(self):
    2626        return self.headline
     27
     28class ArticleWithPKReverseDefaultOrdering(models.Model):
     29    headline = models.CharField(max_length=100)
     30    pub_date = models.DateTimeField()
     31    class Meta:
     32        ordering = ('-pk',)
     33   
     34    def __unicode__(self):
     35        return self.headline
Back to Top