Changes between Version 10 and Version 11 of RemovingTheMagic


Ignore:
Timestamp:
12/13/2005 10:17:54 PM (10 years ago)
Author:
adrian
Comment:

Added status lines

Legend:

Unmodified
Added
Removed
Modified
  • RemovingTheMagic

    v10 v11  
    11= Removing the magic =
    22
    3 This document proposes a new, cleaner and less magical Django database API.
     3The "magic-removal" branch aims to make several sweeping changes to the Django codebase. Most changes involve the database API and removing some of its unneeded magic, which confuses newbies and is a bit of a wart.
    44
    5 == Model definition ==
     5This document explains the changes in the branch.
     6
     7== Models support properties ==
     8
     9'''Status: Done'''
     10
     11Unlike before, properties are supported on models.
     12
     13{{{
     14#!python
     15from django.core import meta
     16
     17class Person(meta.Model):
     18    first_name = meta.CharField(maxlength=30)
     19    last_name = meta.CharField(maxlength=30)
     20
     21    def _get_full_name(self):
     22        return "%s %s" % (self.first_name, self.last_name)
     23    full_name = property(_get_full_name)
     24}}}
     25
     26== Model class and Field classes renamed/relocated ==
     27
     28'''Status: Not yet done'''
    629
    730Difference: Import is from {{{django.db.models}}} instead of {{{django.core.meta}}}. This is easier to remember. However, {{{models}}} may not be the best name for it.
     
    1639}}}
    1740
    18 Unlike before, properties are supported.
     41== Database connection relocated/renamed ==
    1942
    20 {{{
    21 #!python
    22 from django.db import models
     43'''Status: Not yet done'''
    2344
    24 class Person(models.Model):
    25     first_name = models.CharField(maxlength=30)
    26     last_name = models.CharField(maxlength=30)
    27 
    28     def _get_full_name(self):
    29         return "%s %s" % (self.first_name, self.last_name)
    30     full_name = property(_get_full_name)
    31 }}}
    32 
    33 == Database connection ==
     45This is easier to remember, clearer and more consistent.
    3446
    3547Old:
     
    4759}}}
    4860
    49 This is easier to remember, clearer and more consistent.
     61== Interact directly with model classes, not with magic modules ==
    5062
    51 == API usage: Object creation ==
     63'''Status: Done'''
    5264
    5365Import the model class directly from the module in which it was defined. No more {{{django.models.*}}} magic.
     
    6072}}}
    6173
    62 == API usage: Table-level functions ==
     74This also removes the need for the {{{module_name}}} parameter.
    6375
    64 All "table-level" functions -- ways of retrieving records tablewide rather than performing instance-specific tasks -- are accessed via a model class's {{{objects}}} attribute. They aren't direct methods of a model instance object because we want to keep the "table-wide" and "row-specific" namespaces separate.
     76== Access table-level DB API functions via model classes, not with magic modules ==
     77
     78'''Status: Done'''
     79
     80All "table-level" functions -- ways of retrieving records tablewide rather than performing instance-specific tasks -- are now accessed via a model class's {{{objects}}} attribute. They aren't direct methods of a model instance object because we want to keep the "table-wide" and "row-specific" namespaces separate.
    6581
    6682{{{
     
    7692#!python
    7793p = Person.objects.get_object(pk=1)
    78 p.objects.get_list() # Will raise an exception.
     94p.objects.get_list() # Raises AttributeError
    7995}}}
     96
     97== Override default manager name ("objects") ==
     98
     99'''Status: Not yet done'''
    80100
    81101If a model already has an {{{objects}}} attribute, you'll need to specify an alternate name for the magic {{{objects}}}.
     
    90110        manager = Manager(name='more_objects')
    91111
    92 
    93112p = Person(first_name='Mary', last_name='Jones', objects='Hello there.')
    94113p.save()
     
    98117
    99118== API usage: Overriding model methods (and pre- and post-save hooks) ==
     119
     120'''Status: Done'''
    100121
    101122Proper subclassing of methods will now work, so you can subclass the automatic {{{save()}}} and {{{delete()}}} methods. This removes the need for the {{{_pre_save()}}} and {{{_post_save()}}} hooks. Example:
     
    131152== API usage: Overriding table-level functions ==
    132153
     154'''Status: Not yet done'''
     155
    133156You 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.
    134157
     
    151174== Other "module"-level members: Automatic manipulators and !ObjectDoesNotExist exception ==
    152175
     176'''Status: Not yet done'''
     177
    153178{{{
    154179#!python
Back to Top