Changes between Version 14 and Version 15 of RemovingTheMagic


Ignore:
Timestamp:
Dec 14, 2005, 4:18:22 PM (19 years ago)
Author:
Adrian Holovaty
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • RemovingTheMagic

    v14 v15  
    115115== Override default manager name ("objects") ==
    116116
    117 '''Status: Not yet done'''
     117'''Status: Done'''
    118118
    119119If a model already has an {{{objects}}} attribute, you'll need to specify an alternate name for the magic {{{objects}}}.
     
    125125    last_name = models.CharField(maxlength=30)
    126126    objects = models.TextField()
    127     class META:
    128         manager = Manager(name='more_objects')
     127    people = models.Manager()
    129128
    130129p = Person(first_name='Mary', last_name='Jones', objects='Hello there.')
    131130p.save()
    132131p.objects == 'Hello there.'
    133 Person.more_objects.get_list()
     132Person.people.get_list()
     133}}}
     134
     135== Multiple managers ==
     136
     137'''Status: Done'''
     138
     139You can create as many managers as you want. When necessary (such as on the admin), Django will use the first one defined, in order.
     140
     141If you define at least one custom manager, it will not get the default "objects" manager.
     142
     143{{{
     144#!python
     145class Person(models.Model):
     146    first_name = models.CharField(maxlength=30)
     147    last_name = models.CharField(maxlength=30)
     148    people = models.Manager()
     149    fun_people = SomeOtherManager()
    134150}}}
    135151
     
    170186== API usage: Overriding table-level functions ==
    171187
    172 '''Status: Not yet done'''
     188'''Status: Done'''
    173189
    174190You can override any table-level functions, such as {{{get_list()}}} or {{{get_object()}}}. Do this by creating a custom {{{models.Manager}}} subclass and passing it to your model. The term "manager" could be replaced with some other word.
     
    181197        # Changes get_list() to hard-code a limit=10.
    182198        kwargs['limit'] = 10
    183         return super(PersonManager, self).get_list(self, **kwargs) # Call the "real" get_list() method.
    184 
    185 class Person(models.Model):
    186     first_name = models.CharField(maxlength=30)
    187     last_name = models.CharField(maxlength=30)
    188     class META:
    189         manager = PersonManager()
     199        return models.Manager.get_list(self, **kwargs) # Call the "real" get_list() method.
     200
     201class Person(models.Model):
     202    first_name = models.CharField(maxlength=30)
     203    last_name = models.CharField(maxlength=30)
     204    objects = PersonManager()
     205}}}
     206
     207== DoesNotExist exception renamed ==
     208
     209'''Status: Done'''
     210
     211Instead of {{{people.PersonDoesNotExist}}}, it's not {{{Person.DoesNotExist}}}.
     212
     213Old:
     214{{{
     215#!python
     216from django.models.myapp import people
     217try:
     218    people.get_object(pk=1)
     219except people.PersonDoesNotExist:
     220    print "Not there"
     221}}}
     222
     223New:
     224{{{
     225#!python
     226from path.to.myapp import Person
     227try:
     228    Person.objects.get_object(pk=1)
     229except Person.DoesNotExist:
     230    print "Not there"
    190231}}}
    191232
Back to Top