web/lib/django/db/models/options.py
changeset 29 cc9b7e14412b
parent 0 0d40e90630ef
equal deleted inserted replaced
28:b758351d191f 29:cc9b7e14412b
     1 import re
     1 import re
     2 from bisect import bisect
     2 from bisect import bisect
     3 try:
       
     4     set
       
     5 except NameError:
       
     6     from sets import Set as set     # Python 2.3 fallback
       
     7 
     3 
     8 from django.conf import settings
     4 from django.conf import settings
     9 from django.db.models.related import RelatedObject
     5 from django.db.models.related import RelatedObject
    10 from django.db.models.fields.related import ManyToManyRel
     6 from django.db.models.fields.related import ManyToManyRel
    11 from django.db.models.fields import AutoField, FieldDoesNotExist
     7 from django.db.models.fields import AutoField, FieldDoesNotExist
    19 get_verbose_name = lambda class_name: re.sub('(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))', ' \\1', class_name).lower().strip()
    15 get_verbose_name = lambda class_name: re.sub('(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))', ' \\1', class_name).lower().strip()
    20 
    16 
    21 DEFAULT_NAMES = ('verbose_name', 'db_table', 'ordering',
    17 DEFAULT_NAMES = ('verbose_name', 'db_table', 'ordering',
    22                  'unique_together', 'permissions', 'get_latest_by',
    18                  'unique_together', 'permissions', 'get_latest_by',
    23                  'order_with_respect_to', 'app_label', 'db_tablespace',
    19                  'order_with_respect_to', 'app_label', 'db_tablespace',
    24                  'abstract', 'managed', 'proxy')
    20                  'abstract', 'managed', 'proxy', 'auto_created')
    25 
    21 
    26 class Options(object):
    22 class Options(object):
    27     def __init__(self, meta, app_label=None):
    23     def __init__(self, meta, app_label=None):
    28         self.local_fields, self.local_many_to_many = [], []
    24         self.local_fields, self.local_many_to_many = [], []
    29         self.virtual_fields = []
    25         self.virtual_fields = []
    45         self.managed = True
    41         self.managed = True
    46         self.proxy = False
    42         self.proxy = False
    47         self.proxy_for_model = None
    43         self.proxy_for_model = None
    48         self.parents = SortedDict()
    44         self.parents = SortedDict()
    49         self.duplicate_targets = {}
    45         self.duplicate_targets = {}
       
    46         self.auto_created = False
    50 
    47 
    51         # To handle various inheritance situations, we need to track where
    48         # To handle various inheritance situations, we need to track where
    52         # managers came from (concrete or abstract base classes).
    49         # managers came from (concrete or abstract base classes).
    53         self.abstract_managers = []
    50         self.abstract_managers = []
    54         self.concrete_managers = []
    51         self.concrete_managers = []
    91             # by default.
    88             # by default.
    92             setattr(self, 'verbose_name_plural', meta_attrs.pop('verbose_name_plural', string_concat(self.verbose_name, 's')))
    89             setattr(self, 'verbose_name_plural', meta_attrs.pop('verbose_name_plural', string_concat(self.verbose_name, 's')))
    93 
    90 
    94             # Any leftover attributes must be invalid.
    91             # Any leftover attributes must be invalid.
    95             if meta_attrs != {}:
    92             if meta_attrs != {}:
    96                 raise TypeError, "'class Meta' got invalid attribute(s): %s" % ','.join(meta_attrs.keys())
    93                 raise TypeError("'class Meta' got invalid attribute(s): %s" % ','.join(meta_attrs.keys()))
    97         else:
    94         else:
    98             self.verbose_name_plural = string_concat(self.verbose_name, 's')
    95             self.verbose_name_plural = string_concat(self.verbose_name, 's')
    99         del self.meta
    96         del self.meta
   100 
    97 
   101         # If the db_table wasn't provided, use the app_label + module_name.
    98         # If the db_table wasn't provided, use the app_label + module_name.
   102         if not self.db_table:
    99         if not self.db_table:
   103             self.db_table = "%s_%s" % (self.app_label, self.module_name)
   100             self.db_table = "%s_%s" % (self.app_label, self.module_name)
   104             self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
   101             self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
   105 
   102 
   106 
       
   107     def _prepare(self, model):
   103     def _prepare(self, model):
   108         if self.order_with_respect_to:
   104         if self.order_with_respect_to:
   109             self.order_with_respect_to = self.get_field(self.order_with_respect_to)
   105             self.order_with_respect_to = self.get_field(self.order_with_respect_to)
   110             self.ordering = ('_order',)
   106             self.ordering = ('_order',)
       
   107             model.add_to_class('_order', OrderWrt())
   111         else:
   108         else:
   112             self.order_with_respect_to = None
   109             self.order_with_respect_to = None
   113 
   110 
   114         if self.pk is None:
   111         if self.pk is None:
   115             if self.parents:
   112             if self.parents:
   271         """
   268         """
   272         to_search = many_to_many and (self.fields + self.many_to_many) or self.fields
   269         to_search = many_to_many and (self.fields + self.many_to_many) or self.fields
   273         for f in to_search:
   270         for f in to_search:
   274             if f.name == name:
   271             if f.name == name:
   275                 return f
   272                 return f
   276         raise FieldDoesNotExist, '%s has no field named %r' % (self.object_name, name)
   273         raise FieldDoesNotExist('%s has no field named %r' % (self.object_name, name))
   277 
   274 
   278     def get_field_by_name(self, name):
   275     def get_field_by_name(self, name):
   279         """
   276         """
   280         Returns the (field_object, model, direct, m2m), where field_object is
   277         Returns the (field_object, model, direct, m2m), where field_object is
   281         the Field instance for the given name, model is the model containing
   278         the Field instance for the given name, model is the model containing
   327             cache[f.field.related_query_name()] = (f, model, False, False)
   324             cache[f.field.related_query_name()] = (f, model, False, False)
   328         for f, model in self.get_m2m_with_model():
   325         for f, model in self.get_m2m_with_model():
   329             cache[f.name] = (f, model, True, True)
   326             cache[f.name] = (f, model, True, True)
   330         for f, model in self.get_fields_with_model():
   327         for f, model in self.get_fields_with_model():
   331             cache[f.name] = (f, model, True, False)
   328             cache[f.name] = (f, model, True, False)
   332         if self.order_with_respect_to:
       
   333             cache['_order'] = OrderWrt(), None, True, False
       
   334         if app_cache_ready():
   329         if app_cache_ready():
   335             self._name_map = cache
   330             self._name_map = cache
   336         return cache
   331         return cache
   337 
   332 
   338     def get_add_permission(self):
   333     def get_add_permission(self):
   485     def pk_index(self):
   480     def pk_index(self):
   486         """
   481         """
   487         Returns the index of the primary key field in the self.fields list.
   482         Returns the index of the primary key field in the self.fields list.
   488         """
   483         """
   489         return self.fields.index(self.pk)
   484         return self.fields.index(self.pk)
   490