Ticket #122: meta6.diff
File meta6.diff, 8.2 KB (added by , 19 years ago) |
---|
-
django/core/meta.py
393 393 394 394 class ModelBase(type): 395 395 "Metaclass for all models" 396 397 # We need to keep track of Fields which are initiated inside of the class body, but 398 # not assined to an attribute. (These could be ForeignKeys, ManyToManyFields, or 399 # OneToOneFields.) 400 _defined_fields_bank = {} 401 396 402 def __new__(cls, name, bases, attrs): 397 403 # If this isn't a subclass of Model, don't do anything special. 398 404 if not bases: 399 405 return type.__new__(cls, name, bases, attrs) 400 406 407 # We must refactor the attributes around a little. All Field class instances will be given 408 # names (as needed) and moved to the fields list. All attributes of the Meta class will 409 # be moved into the main class attrs. 410 411 attrs["fields"] = attrs.has_key("fields") and list(attrs["fields"]) or [] 412 413 def handle_field(obj, name): 414 if isinstance(obj, Field): #Check if this is a field 415 if isinstance(obj,(ForeignKey, ManyToManyField, OneToOneField)): 416 obj.rel.name = name 417 else: 418 obj.set_name(name) 419 attrs["fields"].append(obj) 420 return True 421 else: 422 return False 423 424 # loop through the attributes, and take out the fields. 425 for key in attrs.keys()[:]: 426 if handle_field(attrs[key], key): 427 del attrs[key] # If the attr was added to fields, we want to delete it. 428 429 if attrs.has_key("Meta"): 430 for name in dir(attrs["Meta"]): 431 if name.startswith("__"): continue 432 if name in ("fields",): 433 attrs[name].extend( getattr(attrs["Meta"],name) ) 434 else: 435 attrs[name] = getattr(attrs["Meta"],name) 436 del attrs["Meta"] 437 438 439 # We don't want to duplicate relationship keys. 440 for field in attrs["fields"]: 441 if ModelBase._defined_fields_bank.has_key(id(field)): 442 del ModelBase._defined_fields_bank[id(field)] 443 444 # Add all of the relationship fields that were not assigned to an attribute. (or 445 # included in the "fields" attribute in the first place.) 446 attrs["fields"].extend( ModelBase._defined_fields_bank.values() ) 447 ModelBase._defined_fields_bank = {} 448 449 # Sort the fields, so that they appear in the correct order. 450 attrs["fields"].sort(lambda x,y: x.creation_counter - y.creation_counter) 451 401 452 # If this model is a subclass of another Model, create an Options 402 453 # object by first copying the base class's _meta and then updating it 403 454 # with the overrides from this class. … … 1572 1623 # database level. 1573 1624 empty_strings_allowed = True 1574 1625 1575 def __init__(self, name, verbose_name=None, primary_key=False, 1626 # Will be increased each time a Field object is instanced. Used to 1627 # retain the order of fields. 1628 creation_counter = 0 1629 1630 def __init__(self, name=None, verbose_name=None, primary_key=False, 1576 1631 maxlength=None, unique=False, blank=False, null=False, db_index=None, 1577 1632 core=False, rel=None, default=NOT_PROVIDED, editable=True, 1578 1633 prepopulate_from=None, unique_for_date=None, unique_for_month=None, 1579 1634 unique_for_year=None, validator_list=None, choices=None, radio_admin=None, 1580 1635 help_text=''): 1581 1636 self.name = name 1582 self.verbose_name = verbose_name or name.replace('_', ' ') 1637 self.original_verbose_name = verbose_name 1638 self.verbose_name = verbose_name or name and name.replace('_', ' ') 1583 1639 self.primary_key = primary_key 1584 1640 self.maxlength, self.unique = maxlength, unique 1585 1641 self.blank, self.null = blank, null … … 1604 1660 else: 1605 1661 self.db_index = db_index 1606 1662 1663 # Increase the creation counter, and save our local copy. 1664 self.creation_counter = Field.creation_counter 1665 Field.creation_counter += 1 1666 1667 def set_name(self, name ): 1668 """ 1669 Sets the name, and generates the verbose_name from it if needed. 1670 This function is here for when the name needs to be set later, (such as if it needs to be obtained from 1671 a the attribute name that stores the Field instance.) 1672 """ 1673 if not self.original_verbose_name: 1674 # If the verbose name was originally not specified, we will assume that 1675 # the user intends for the first argument passed to __init__ to be the verbose_name. 1676 self.verbose_name = self.name 1677 1678 self.name = name 1679 self.verbose_name = self.verbose_name or name and name.replace('_', ' ') 1680 1607 1681 def pre_save(self, obj, value, add): 1608 1682 """ 1609 1683 Hook for altering the object obj based on the value of this field and … … 1789 1863 1790 1864 class DateField(Field): 1791 1865 empty_strings_allowed = False 1792 def __init__(self, name , verbose_name=None, auto_now=False, auto_now_add=False, **kwargs):1866 def __init__(self, name=None, verbose_name=None, auto_now=False, auto_now_add=False, **kwargs): 1793 1867 self.auto_now, self.auto_now_add = auto_now, auto_now_add 1794 1868 if auto_now or auto_now_add: 1795 1869 kwargs['editable'] = False … … 1845 1919 return [formfields.EmailField] 1846 1920 1847 1921 class FileField(Field): 1848 def __init__(self, name , verbose_name=None, upload_to='', **kwargs):1922 def __init__(self, name=None, verbose_name=None, upload_to='', **kwargs): 1849 1923 self.upload_to = upload_to 1850 1924 Field.__init__(self, name, verbose_name, **kwargs) 1851 1925 … … 1910 1984 1911 1985 class FloatField(Field): 1912 1986 empty_strings_allowed = False 1913 def __init__(self, name , verbose_name=None, max_digits=None, decimal_places=None, **kwargs):1987 def __init__(self, name=None, verbose_name=None, max_digits=None, decimal_places=None, **kwargs): 1914 1988 self.max_digits, self.decimal_places = max_digits, decimal_places 1915 1989 Field.__init__(self, name, verbose_name, **kwargs) 1916 1990 … … 1992 2066 1993 2067 class TimeField(Field): 1994 2068 empty_strings_allowed = False 1995 def __init__(self, name , verbose_name=None, auto_now=False, auto_now_add=False, **kwargs):2069 def __init__(self, name=None, verbose_name=None, auto_now=False, auto_now_add=False, **kwargs): 1996 2070 self.auto_now, self.auto_now_add = auto_now, auto_now_add 1997 2071 if auto_now or auto_now_add: 1998 2072 kwargs['editable'] = False … … 2019 2093 return [formfields.TimeField] 2020 2094 2021 2095 class URLField(Field): 2022 def __init__(self, name , verbose_name=None, verify_exists=True, **kwargs):2096 def __init__(self, name=None, verbose_name=None, verify_exists=True, **kwargs): 2023 2097 if verify_exists: 2024 2098 kwargs.setdefault('validator_list', []).append(validators.isExistingURL) 2025 2099 Field.__init__(self, name, verbose_name, **kwargs) … … 2032 2106 return [formfields.USStateField] 2033 2107 2034 2108 class XMLField(Field): 2035 def __init__(self, name , verbose_name=None, schema_path=None, **kwargs):2109 def __init__(self, name=None, verbose_name=None, schema_path=None, **kwargs): 2036 2110 self.schema_path = schema_path 2037 2111 Field.__init__(self, name, verbose_name, **kwargs) 2038 2112 … … 2065 2139 lookup_overrides=kwargs.pop('lookup_overrides', None), 2066 2140 raw_id_admin=kwargs.pop('raw_id_admin', False)) 2067 2141 Field.__init__(self, **kwargs) 2142 ModelBase._defined_fields_bank[id(self)]=self 2068 2143 2069 2144 def get_manipulator_field_objs(self): 2070 2145 return [formfields.IntegerField] … … 2080 2155 filter_interface=kwargs.pop('filter_interface', None), 2081 2156 limit_choices_to=kwargs.pop('limit_choices_to', None)) 2082 2157 Field.__init__(self, **kwargs) 2158 ModelBase._defined_fields_bank[id(self)]=self 2083 2159 2084 2160 def get_manipulator_field_objs(self): 2085 2161 choices = self.get_choices(include_blank=False) … … 2105 2181 raw_id_admin=kwargs.pop('raw_id_admin', False)) 2106 2182 kwargs['primary_key'] = True 2107 2183 IntegerField.__init__(self, **kwargs) 2184 ModelBase._defined_fields_bank[id(self)]=self 2108 2185 2109 2186 #################### 2110 2187 # RELATIONSHIPS #