Ticket #8291: 8291_2.diff

File 8291_2.diff, 2.2 KB (added by David Gouldin, 16 years ago)
  • django/db/models/options.py

     
    253253        self._m2m_cache = cache
    254254
    255255    def get_field(self, name, many_to_many=True):
     256        if name == 'pk': # replace special value "pk" with primary key attribute name
     257            attname = self.pk.attname
     258        else:
     259            attname = name
    256260        """
    257261        Returns the requested field by name. Raises FieldDoesNotExist on error.
    258262        """
    259263        to_search = many_to_many and (self.fields + self.many_to_many) or self.fields
    260264        for f in to_search:
    261             if f.name == name:
     265            if f.name == attname:
    262266                return f
    263267        raise FieldDoesNotExist, '%s has no field named %r' % (self.object_name, name)
    264268
  • tests/modeltests/ordering/models.py

     
    2424    def __unicode__(self):
    2525        return self.headline
    2626
     27class Writer(models.Model):
     28    name = models.CharField(max_length=50)
     29    class Meta:
     30        ordering = ('pk',)
     31
     32    def __unicode__(self):
     33        return self.name
     34
    2735__test__ = {'API_TESTS':"""
    2836# Create a couple of Articles.
    2937>>> from datetime import datetime
     
    3644>>> a4 = Article(headline='Article 4', pub_date=datetime(2005, 7, 28))
    3745>>> a4.save()
    3846
     47# Create a couple of Writers.
     48>>> w1 = Writer(name='Kurt Vonnegut')
     49>>> w1.save()
     50>>> w2 = Writer(name='Joseph Heller')
     51>>> w2.save()
     52>>> w3 = Writer(name='Douglas Adams')
     53>>> w3.save()
     54
    3955# By default, Article.objects.all() orders by pub_date descending, then
    4056# headline ascending.
    4157>>> Article.objects.all()
     
    86102>>> Article.objects.extra(select={'order': 'pub_date'}, order_by=['order', 'headline'])
    87103[<Article: Article 1>, <Article: Article 2>, <Article: Article 3>, <Article: Article 4>]
    88104
     105# Use the special value 'pk' to order on the primary key field
     106>>> Writer.objects.order_by('pk')
     107[<Writer: Kurt Vonnegut>, <Writer: Joseph Heller>, <Writer: Douglas Adams>]
    89108"""}
Back to Top