Ticket #122: meta4.diff
File meta4.diff, 6.4 KB (added by , 19 years ago) |
---|
-
django/core/meta.py
398 398 if not bases: 399 399 return type.__new__(cls, name, bases, attrs) 400 400 401 # We must refactor the attributes around a little. All Field class instances will be given 402 # names (as needed) and moved to the fields list. All attributes of the Meta class will 403 # be moved into the main class attrs. 404 405 attrs["fields"] = attrs.has_key("fields") and list(attrs["fields"]) or [] 406 407 def handle_field(obj, name): 408 if isinstance(obj, Field): #Check if this is a field 409 if isinstance(obj,(ForeignKey, ManyToManyField, OneToOneField)): 410 obj.rel.name = name 411 else: 412 obj.set_name(name) 413 attrs["fields"].append(obj) 414 return True 415 else: 416 return False 417 418 # loop through the attributes, and take out the fields. 419 for key in attrs.keys()[:]: 420 if handle_field(attrs[key], key): 421 del attrs[key] # If the attr was added to fields, we want to delete it. 422 423 if attrs.has_key("Meta"): 424 for name in dir(attrs["Meta"]): 425 if name.startswith("__"): continue 426 if name in ("fields",): 427 attrs[name].extend( getattr(attrs["Meta"],name) ) 428 else: 429 attrs[name] = getattr(attrs["Meta"],name) 430 del attrs["Meta"] 431 432 # Sort the fields, so that they appear in the correct order. 433 attrs["fields"].sort(lambda x,y: x.creation_counter - y.creation_counter) 434 401 435 # If this model is a subclass of another Model, create an Options 402 436 # object by first copying the base class's _meta and then updating it 403 437 # with the overrides from this class. … … 1572 1606 # database level. 1573 1607 empty_strings_allowed = True 1574 1608 1575 def __init__(self, name, verbose_name=None, primary_key=False, 1609 # Will be increased each time a Field object is instanced. Used to 1610 # retain the order of fields. 1611 creation_counter = 0 1612 1613 def __init__(self, name=None, verbose_name=None, primary_key=False, 1576 1614 maxlength=None, unique=False, blank=False, null=False, db_index=None, 1577 1615 core=False, rel=None, default=NOT_PROVIDED, editable=True, 1578 1616 prepopulate_from=None, unique_for_date=None, unique_for_month=None, 1579 1617 unique_for_year=None, validator_list=None, choices=None, radio_admin=None, 1580 1618 help_text=''): 1581 1619 self.name = name 1582 self.verbose_name = verbose_name or name.replace('_', ' ') 1620 self.original_verbose_name = verbose_name 1621 self.verbose_name = verbose_name or name and name.replace('_', ' ') 1583 1622 self.primary_key = primary_key 1584 1623 self.maxlength, self.unique = maxlength, unique 1585 1624 self.blank, self.null = blank, null … … 1604 1643 else: 1605 1644 self.db_index = db_index 1606 1645 1646 # Increase the creation counter, and save our local copy. 1647 self.creation_counter = Field.creation_counter 1648 Field.creation_counter += 1 1649 1650 def set_name(self, name ): 1651 """ 1652 Sets the name, and generates the verbose_name from it if needed. 1653 This function is here for when the name needs to be set later, (such as if it needs to be obtained from 1654 a the attribute name that stores the Field instance.) 1655 """ 1656 if not self.original_verbose_name: 1657 # If the verbose name was originally not specified, we will assume that 1658 # the user intends for the first argument passed to __init__ to be the verbose_name. 1659 self.verbose_name = self.name 1660 1661 self.name = name 1662 self.verbose_name = self.verbose_name or name and name.replace('_', ' ') 1663 1607 1664 def pre_save(self, obj, value, add): 1608 1665 """ 1609 1666 Hook for altering the object obj based on the value of this field and … … 1789 1846 1790 1847 class DateField(Field): 1791 1848 empty_strings_allowed = False 1792 def __init__(self, name , verbose_name=None, auto_now=False, auto_now_add=False, **kwargs):1849 def __init__(self, name=None, verbose_name=None, auto_now=False, auto_now_add=False, **kwargs): 1793 1850 self.auto_now, self.auto_now_add = auto_now, auto_now_add 1794 1851 if auto_now or auto_now_add: 1795 1852 kwargs['editable'] = False … … 1845 1902 return [formfields.EmailField] 1846 1903 1847 1904 class FileField(Field): 1848 def __init__(self, name , verbose_name=None, upload_to='', **kwargs):1905 def __init__(self, name=None, verbose_name=None, upload_to='', **kwargs): 1849 1906 self.upload_to = upload_to 1850 1907 Field.__init__(self, name, verbose_name, **kwargs) 1851 1908 … … 1910 1967 1911 1968 class FloatField(Field): 1912 1969 empty_strings_allowed = False 1913 def __init__(self, name , verbose_name=None, max_digits=None, decimal_places=None, **kwargs):1970 def __init__(self, name=None, verbose_name=None, max_digits=None, decimal_places=None, **kwargs): 1914 1971 self.max_digits, self.decimal_places = max_digits, decimal_places 1915 1972 Field.__init__(self, name, verbose_name, **kwargs) 1916 1973 … … 1992 2049 1993 2050 class TimeField(Field): 1994 2051 empty_strings_allowed = False 1995 def __init__(self, name , verbose_name=None, auto_now=False, auto_now_add=False, **kwargs):2052 def __init__(self, name=None, verbose_name=None, auto_now=False, auto_now_add=False, **kwargs): 1996 2053 self.auto_now, self.auto_now_add = auto_now, auto_now_add 1997 2054 if auto_now or auto_now_add: 1998 2055 kwargs['editable'] = False … … 2019 2076 return [formfields.TimeField] 2020 2077 2021 2078 class URLField(Field): 2022 def __init__(self, name , verbose_name=None, verify_exists=True, **kwargs):2079 def __init__(self, name=None, verbose_name=None, verify_exists=True, **kwargs): 2023 2080 if verify_exists: 2024 2081 kwargs.setdefault('validator_list', []).append(validators.isExistingURL) 2025 2082 Field.__init__(self, name, verbose_name, **kwargs) … … 2032 2089 return [formfields.USStateField] 2033 2090 2034 2091 class XMLField(Field): 2035 def __init__(self, name , verbose_name=None, schema_path=None, **kwargs):2092 def __init__(self, name=None, verbose_name=None, schema_path=None, **kwargs): 2036 2093 self.schema_path = schema_path 2037 2094 Field.__init__(self, name, verbose_name, **kwargs) 2038 2095