Ticket #122: meta5.diff
File meta5.diff, 8.1 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 # We need to delete this Field from the dict, as it was assigned 418 # to an attribute. (We don't want to add it again later.) 419 del ModelBase._defined_fields_bank[id(obj)] 420 else: 421 obj.set_name(name) 422 attrs["fields"].append(obj) 423 return True 424 else: 425 return False 426 427 # loop through the attributes, and take out the fields. 428 for key in attrs.keys()[:]: 429 if handle_field(attrs[key], key): 430 del attrs[key] # If the attr was added to fields, we want to delete it. 431 432 # Add all of the relationship fields that were not assigned to an attribute. 433 attrs["fields"].extend( ModelBase._defined_fields_bank.values() ) 434 ModelBase._defined_fields_bank = {} 435 436 if attrs.has_key("Meta"): 437 for name in dir(attrs["Meta"]): 438 if name.startswith("__"): continue 439 if name in ("fields",): 440 attrs[name].extend( getattr(attrs["Meta"],name) ) 441 else: 442 attrs[name] = getattr(attrs["Meta"],name) 443 del attrs["Meta"] 444 445 # Sort the fields, so that they appear in the correct order. 446 attrs["fields"].sort(lambda x,y: x.creation_counter - y.creation_counter) 447 401 448 # If this model is a subclass of another Model, create an Options 402 449 # object by first copying the base class's _meta and then updating it 403 450 # with the overrides from this class. … … 1572 1619 # database level. 1573 1620 empty_strings_allowed = True 1574 1621 1575 def __init__(self, name, verbose_name=None, primary_key=False, 1622 # Will be increased each time a Field object is instanced. Used to 1623 # retain the order of fields. 1624 creation_counter = 0 1625 1626 def __init__(self, name=None, verbose_name=None, primary_key=False, 1576 1627 maxlength=None, unique=False, blank=False, null=False, db_index=None, 1577 1628 core=False, rel=None, default=NOT_PROVIDED, editable=True, 1578 1629 prepopulate_from=None, unique_for_date=None, unique_for_month=None, 1579 1630 unique_for_year=None, validator_list=None, choices=None, radio_admin=None, 1580 1631 help_text=''): 1581 1632 self.name = name 1582 self.verbose_name = verbose_name or name.replace('_', ' ') 1633 self.original_verbose_name = verbose_name 1634 self.verbose_name = verbose_name or name and name.replace('_', ' ') 1583 1635 self.primary_key = primary_key 1584 1636 self.maxlength, self.unique = maxlength, unique 1585 1637 self.blank, self.null = blank, null … … 1604 1656 else: 1605 1657 self.db_index = db_index 1606 1658 1659 # Increase the creation counter, and save our local copy. 1660 self.creation_counter = Field.creation_counter 1661 Field.creation_counter += 1 1662 1663 def set_name(self, name ): 1664 """ 1665 Sets the name, and generates the verbose_name from it if needed. 1666 This function is here for when the name needs to be set later, (such as if it needs to be obtained from 1667 a the attribute name that stores the Field instance.) 1668 """ 1669 if not self.original_verbose_name: 1670 # If the verbose name was originally not specified, we will assume that 1671 # the user intends for the first argument passed to __init__ to be the verbose_name. 1672 self.verbose_name = self.name 1673 1674 self.name = name 1675 self.verbose_name = self.verbose_name or name and name.replace('_', ' ') 1676 1607 1677 def pre_save(self, obj, value, add): 1608 1678 """ 1609 1679 Hook for altering the object obj based on the value of this field and … … 1789 1859 1790 1860 class DateField(Field): 1791 1861 empty_strings_allowed = False 1792 def __init__(self, name , verbose_name=None, auto_now=False, auto_now_add=False, **kwargs):1862 def __init__(self, name=None, verbose_name=None, auto_now=False, auto_now_add=False, **kwargs): 1793 1863 self.auto_now, self.auto_now_add = auto_now, auto_now_add 1794 1864 if auto_now or auto_now_add: 1795 1865 kwargs['editable'] = False … … 1845 1915 return [formfields.EmailField] 1846 1916 1847 1917 class FileField(Field): 1848 def __init__(self, name , verbose_name=None, upload_to='', **kwargs):1918 def __init__(self, name=None, verbose_name=None, upload_to='', **kwargs): 1849 1919 self.upload_to = upload_to 1850 1920 Field.__init__(self, name, verbose_name, **kwargs) 1851 1921 … … 1910 1980 1911 1981 class FloatField(Field): 1912 1982 empty_strings_allowed = False 1913 def __init__(self, name , verbose_name=None, max_digits=None, decimal_places=None, **kwargs):1983 def __init__(self, name=None, verbose_name=None, max_digits=None, decimal_places=None, **kwargs): 1914 1984 self.max_digits, self.decimal_places = max_digits, decimal_places 1915 1985 Field.__init__(self, name, verbose_name, **kwargs) 1916 1986 … … 1992 2062 1993 2063 class TimeField(Field): 1994 2064 empty_strings_allowed = False 1995 def __init__(self, name , verbose_name=None, auto_now=False, auto_now_add=False, **kwargs):2065 def __init__(self, name=None, verbose_name=None, auto_now=False, auto_now_add=False, **kwargs): 1996 2066 self.auto_now, self.auto_now_add = auto_now, auto_now_add 1997 2067 if auto_now or auto_now_add: 1998 2068 kwargs['editable'] = False … … 2019 2089 return [formfields.TimeField] 2020 2090 2021 2091 class URLField(Field): 2022 def __init__(self, name , verbose_name=None, verify_exists=True, **kwargs):2092 def __init__(self, name=None, verbose_name=None, verify_exists=True, **kwargs): 2023 2093 if verify_exists: 2024 2094 kwargs.setdefault('validator_list', []).append(validators.isExistingURL) 2025 2095 Field.__init__(self, name, verbose_name, **kwargs) … … 2032 2102 return [formfields.USStateField] 2033 2103 2034 2104 class XMLField(Field): 2035 def __init__(self, name , verbose_name=None, schema_path=None, **kwargs):2105 def __init__(self, name=None, verbose_name=None, schema_path=None, **kwargs): 2036 2106 self.schema_path = schema_path 2037 2107 Field.__init__(self, name, verbose_name, **kwargs) 2038 2108 … … 2065 2135 lookup_overrides=kwargs.pop('lookup_overrides', None), 2066 2136 raw_id_admin=kwargs.pop('raw_id_admin', False)) 2067 2137 Field.__init__(self, **kwargs) 2138 ModelBase._defined_fields_bank[id(self)]=self 2068 2139 2069 2140 def get_manipulator_field_objs(self): 2070 2141 return [formfields.IntegerField] … … 2080 2151 filter_interface=kwargs.pop('filter_interface', None), 2081 2152 limit_choices_to=kwargs.pop('limit_choices_to', None)) 2082 2153 Field.__init__(self, **kwargs) 2154 ModelBase._defined_fields_bank[id(self)]=self 2083 2155 2084 2156 def get_manipulator_field_objs(self): 2085 2157 choices = self.get_choices(include_blank=False) … … 2105 2177 raw_id_admin=kwargs.pop('raw_id_admin', False)) 2106 2178 kwargs['primary_key'] = True 2107 2179 IntegerField.__init__(self, **kwargs) 2180 ModelBase._defined_fields_bank[id(self)]=self 2108 2181 2109 2182 #################### 2110 2183 # RELATIONSHIPS #