Ticket #122: meta4.diff

File meta4.diff, 6.4 KB (added by mmarshall, 19 years ago)

Patch version 4... Cuts out a bunch of the options.

  • django/core/meta.py

     
    398398        if not bases:
    399399            return type.__new__(cls, name, bases, attrs)
    400400
     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
    401435        # If this model is a subclass of another Model, create an Options
    402436        # object by first copying the base class's _meta and then updating it
    403437        # with the overrides from this class.
     
    15721606    # database level.
    15731607    empty_strings_allowed = True
    15741608
    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,
    15761614        maxlength=None, unique=False, blank=False, null=False, db_index=None,
    15771615        core=False, rel=None, default=NOT_PROVIDED, editable=True,
    15781616        prepopulate_from=None, unique_for_date=None, unique_for_month=None,
    15791617        unique_for_year=None, validator_list=None, choices=None, radio_admin=None,
    15801618        help_text=''):
    15811619        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('_', ' ')
    15831622        self.primary_key = primary_key
    15841623        self.maxlength, self.unique = maxlength, unique
    15851624        self.blank, self.null = blank, null
     
    16041643        else:
    16051644            self.db_index = db_index
    16061645
     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           
    16071664    def pre_save(self, obj, value, add):
    16081665        """
    16091666        Hook for altering the object obj based on the value of this field and
     
    17891846
    17901847class DateField(Field):
    17911848    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):
    17931850        self.auto_now, self.auto_now_add = auto_now, auto_now_add
    17941851        if auto_now or auto_now_add:
    17951852            kwargs['editable'] = False
     
    18451902        return [formfields.EmailField]
    18461903
    18471904class 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):
    18491906        self.upload_to = upload_to
    18501907        Field.__init__(self, name, verbose_name, **kwargs)
    18511908
     
    19101967
    19111968class FloatField(Field):
    19121969    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):
    19141971        self.max_digits, self.decimal_places = max_digits, decimal_places
    19151972        Field.__init__(self, name, verbose_name, **kwargs)
    19161973
     
    19922049
    19932050class TimeField(Field):
    19942051    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):
    19962053        self.auto_now, self.auto_now_add  = auto_now, auto_now_add
    19972054        if auto_now or auto_now_add:
    19982055            kwargs['editable'] = False
     
    20192076        return [formfields.TimeField]
    20202077
    20212078class 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):
    20232080        if verify_exists:
    20242081            kwargs.setdefault('validator_list', []).append(validators.isExistingURL)
    20252082        Field.__init__(self, name, verbose_name, **kwargs)
     
    20322089        return [formfields.USStateField]
    20332090
    20342091class 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):
    20362093        self.schema_path = schema_path
    20372094        Field.__init__(self, name, verbose_name, **kwargs)
    20382095
Back to Top