Changes between Version 10 and Version 11 of RemovingTheMagic
- Timestamp:
- Dec 13, 2005, 10:17:54 PM (19 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
RemovingTheMagic
v10 v11 1 1 = Removing the magic = 2 2 3 Th is document proposes a new, cleaner and less magical Django database API.3 The "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. 4 4 5 == Model definition == 5 This document explains the changes in the branch. 6 7 == Models support properties == 8 9 '''Status: Done''' 10 11 Unlike before, properties are supported on models. 12 13 {{{ 14 #!python 15 from django.core import meta 16 17 class 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''' 6 29 7 30 Difference: 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. … … 16 39 }}} 17 40 18 Unlike before, properties are supported. 41 == Database connection relocated/renamed == 19 42 20 {{{ 21 #!python 22 from django.db import models 43 '''Status: Not yet done''' 23 44 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 == 45 This is easier to remember, clearer and more consistent. 34 46 35 47 Old: … … 47 59 }}} 48 60 49 This is easier to remember, clearer and more consistent. 61 == Interact directly with model classes, not with magic modules == 50 62 51 == API usage: Object creation == 63 '''Status: Done''' 52 64 53 65 Import the model class directly from the module in which it was defined. No more {{{django.models.*}}} magic. … … 60 72 }}} 61 73 62 == API usage: Table-level functions == 74 This also removes the need for the {{{module_name}}} parameter. 63 75 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 80 All "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. 65 81 66 82 {{{ … … 76 92 #!python 77 93 p = Person.objects.get_object(pk=1) 78 p.objects.get_list() # Will raise an exception.94 p.objects.get_list() # Raises AttributeError 79 95 }}} 96 97 == Override default manager name ("objects") == 98 99 '''Status: Not yet done''' 80 100 81 101 If a model already has an {{{objects}}} attribute, you'll need to specify an alternate name for the magic {{{objects}}}. … … 90 110 manager = Manager(name='more_objects') 91 111 92 93 112 p = Person(first_name='Mary', last_name='Jones', objects='Hello there.') 94 113 p.save() … … 98 117 99 118 == API usage: Overriding model methods (and pre- and post-save hooks) == 119 120 '''Status: Done''' 100 121 101 122 Proper 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: … … 131 152 == API usage: Overriding table-level functions == 132 153 154 '''Status: Not yet done''' 155 133 156 You 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. 134 157 … … 151 174 == Other "module"-level members: Automatic manipulators and !ObjectDoesNotExist exception == 152 175 176 '''Status: Not yet done''' 177 153 178 {{{ 154 179 #!python