Changeset 1712
- Timestamp:
- 12/16/05 17:34:44 (3 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/magic-removal/django/core/management.py
r1709 r1712 7 7 from optparse import OptionParser 8 8 9 # HACK: for Python2.310 if not hasattr(__builtins__, 'set'):9 # For Python 2.3 10 if not hasattr(__builtins__, 'set'): 11 11 from sets import Set as set 12 12 django/branches/magic-removal/django/db/models/base.py
r1708 r1712 4 4 from django.db.models.related import RelatedObject 5 5 from django.db.models.manager import Manager, ManagerDescriptor 6 from django.db.models.query import orderlist2sql 6 from django.db.models.query import orderlist2sql 7 7 from django.db.models.options import Options 8 8 from django.db import connection, backend … … 15 15 import sys 16 16 17 # HACK: for Python2.318 if not hasattr(__builtins__, 'set'):17 # For Python 2.3 18 if not hasattr(__builtins__, 'set'): 19 19 from sets import Set as set 20 20 … … 42 42 # Create the class, because we need it to use in currying. 43 43 new_class = type.__new__(cls, name, bases, { '__module__' : attrs.pop('__module__') }) 44 44 45 45 opts = Options( 46 46 module_name = meta_attrs.pop('module_name', get_module_name(name)), … … 66 66 raise TypeError, "'class META' got invalid attribute(s): %s" % ','.join(meta_attrs.keys()) 67 67 new_class.add_to_class('_meta', opts) 68 68 69 69 # Create the DoesNotExist exception. 70 70 new_class.DoesNotExist = types.ClassType('DoesNotExist', (ObjectDoesNotExist,), {}) 71 71 72 72 # Figure out the app_label by looking one level up. 73 73 #FIXME: wrong for nested model modules … … 78 78 # Cache the app label. 79 79 opts.app_label = app_label 80 80 81 81 #Add all attributes to the class 82 82 #fields, managers = [], [] 83 83 for obj_name, obj in attrs.items(): 84 84 new_class.add_to_class(obj_name, obj) 85 85 86 86 if not hasattr(new_class, '_default_manager'): 87 87 # Create the default manager, if needed. … … 89 89 raise ValueError, "Model %s must specify a custom Manager, because it has a field named 'objects'" % name 90 90 new_class.add_to_class('objects', Manager()) 91 91 92 92 # Give the class a docstring -- its definition. 93 93 if new_class.__doc__ is None: … … 96 96 if hasattr(new_class, 'get_absolute_url'): 97 97 new_class.get_absolute_url = curry(get_absolute_url, opts, new_class.get_absolute_url) 98 98 99 99 opts._prepare() 100 100 new_class._prepare() 101 101 102 102 # If the db_table wasn't provided, use the app_label + module_name. 103 103 if not opts.db_table: 104 104 opts.db_table = "%s_%s" % (app_label, opts.module_name) 105 105 106 106 # Populate the _MODELS member on the module the class is in. 107 107 app_package.__dict__.setdefault('_MODELS', []).append(new_class) 108 109 108 109 110 110 return new_class 111 111 112 112 def cmp_cls(x, y): 113 113 for field in x._meta.fields: … … 121 121 class Model(object): 122 122 __metaclass__ = ModelBase 123 123 124 124 def add_to_class(cls, name, attribute): 125 125 if hasattr(attribute, 'contribute_to_class'): … … 128 128 setattr(cls, name, attribute) 129 129 add_to_class = classmethod(add_to_class) 130 130 131 131 AddManipulator = ManipulatorDescriptor('AddManipulator', ModelAddManipulator) 132 ChangeManipulator = ManipulatorDescriptor('ChangeManipulator', ModelChangeManipulator) 133 132 ChangeManipulator = ManipulatorDescriptor('ChangeManipulator', ModelChangeManipulator) 133 134 134 def __repr__(self): 135 135 return '<%s object>' % self.__class__.__name__ … … 215 215 cls.get_next_in_order = curry(cls.__get_next_or_previous_in_order, is_next=True) 216 216 cls.get_previous_in_order = curry(cls.__get_next_or_previous_in_order, is_next=False) 217 217 218 218 RelatedField.do_pending_lookups(cls) 219 219 … … 278 278 def __collect_sub_objects(self, seen_objs, ignore_objs): 279 279 pk_val = self.__get_pk_val() 280 281 key = (self.__class__, pk_val) 282 280 281 key = (self.__class__, pk_val) 282 283 283 if key in seen_objs or key in ignore_objs: 284 284 return 285 285 seen_objs[key] = self 286 286 287 287 for related in self._meta.get_all_related_objects(): 288 288 rel_opts_name = related.get_method_name_part() … … 302 302 ignore_objects = \ 303 303 ignore_objects and dict([ (o.__class,o.__get_pk_val) for o in ignore_objects ]) or {} 304 304 305 305 seen_objs = {} 306 306 self.__collect_sub_objects(seen_objs, ignore_objects) 307 307 308 308 seen_cls = set([cls for cls,pk in seen_objs.keys()]) 309 309 cls_order = list(seen_cls) … … 312 312 seen_tups = [ (cls, pk_val, instance) for (cls, pk_val),instance in seen_objs.items() ] 313 313 seen_tups.sort(lambda x,y: cmp(cls_order.index(x[0]), cls_order.index(y[0]))) 314 314 315 315 cursor = connection.cursor() 316 316 317 317 for cls, pk_val, instance in seen_tups: 318 318 319 319 # Run any pre-delete hooks. 320 320 if hasattr(instance, '_pre_delete'): 321 321 instance._pre_delete() 322 322 323 323 for related in cls._meta.get_all_related_many_to_many_objects(): 324 324 cursor.execute("DELETE FROM %s WHERE %s=%%s" % \ … … 331 331 backend.quote_name(cls._meta.object_name.lower() + '_id')), 332 332 [pk_val]) 333 333 334 334 for field in cls._meta.fields: 335 335 if field.rel and field.null and field.rel.to in seen_cls: 336 336 cursor.execute("UPDATE %s SET %s = NULL WHERE %s =%%s" % \ 337 ( backend.quote_name(cls._meta.db_table), 337 ( backend.quote_name(cls._meta.db_table), 338 338 backend.quote_name(field.column), 339 339 backend.quote_name(cls._meta.pk.column)), 340 340 [pk_val] ) 341 341 342 342 seen_tups.reverse() 343 343 344 344 for cls, pk_val, instance in seen_tups: 345 345 cursor.execute("DELETE FROM %s WHERE %s=%%s" % \ 346 346 (backend.quote_name(cls._meta.db_table), backend.quote_name(cls._meta.pk.column)), 347 347 [pk_val]) 348 349 348 350 349 setattr(self, cls._meta.pk.attname, None) 351 350 for f in cls._meta.fields: … … 359 358 if hasattr(instance, '_post_delete'): 360 359 instance._post_delete() 361 360 362 361 connection.commit() 363 362 364 363 delete.alters_data = True 365 364 366 365 367 366 def __get_FIELD_display(self, field): 368 367 value = getattr(self, field.attname) … … 573 572 574 573 575 574 576 575 577 576
