Ticket #1559: always_process_defaults.diff

File always_process_defaults.diff, 2.9 KB (added by jim-django@…, 19 years ago)

Always process field defaults in django.core.meta.method_init

  • __init__.py

     
    944944# CORE METHODS #############################
    945945
    946946def method_init(opts, self, *args, **kwargs):
    947     if kwargs:
    948         for f in opts.fields:
    949             if isinstance(f.rel, ManyToOne):
     947    for f in opts.fields:
     948        if isinstance(f.rel, ManyToOne):
     949            try:
     950                # Assume object instance was passed in.
     951                rel_obj = kwargs.pop(f.name)
     952            except KeyError:
    950953                try:
    951                     # Assume object instance was passed in.
    952                     rel_obj = kwargs.pop(f.name)
     954                    # Object instance wasn't passed in -- must be an ID.
     955                    val = kwargs.pop(f.attname)
    953956                except KeyError:
    954                     try:
    955                         # Object instance wasn't passed in -- must be an ID.
    956                         val = kwargs.pop(f.attname)
    957                     except KeyError:
    958                         val = f.get_default()
    959                 else:
    960                     # Object instance was passed in.
    961                     # Special case: You can pass in "None" for related objects if it's allowed.
    962                     if rel_obj is None and f.null:
    963                         val = None
    964                     else:
    965                         try:
    966                             val = getattr(rel_obj, f.rel.get_related_field().attname)
    967                         except AttributeError:
    968                             raise TypeError, "Invalid value: %r should be a %s instance, not a %s" % (f.name, f.rel.to, type(rel_obj))
    969                 setattr(self, f.attname, val)
     957                    val = f.get_default()
    970958            else:
    971                 val = kwargs.pop(f.attname, f.get_default())
    972                 setattr(self, f.attname, val)
    973         if kwargs:
    974             raise TypeError, "'%s' is an invalid keyword argument for this function" % kwargs.keys()[0]
     959                # Object instance was passed in.
     960                # Special case: You can pass in "None" for related objects if it's allowed.
     961                if rel_obj is None and f.null:
     962                    val = None
     963                else:
     964                    try:
     965                        val = getattr(rel_obj, f.rel.get_related_field().attname)
     966                    except AttributeError:
     967                        raise TypeError, "Invalid value: %r should be a %s instance, not a %s" % (f.name, f.rel.to, type(rel_obj))
     968            setattr(self, f.attname, val)
     969        else:
     970            val = kwargs.pop(f.attname, f.get_default())
     971            setattr(self, f.attname, val)
     972    if kwargs:
     973        raise TypeError, "'%s' is an invalid keyword argument for this function" % kwargs.keys()[0]
    975974    for i, arg in enumerate(args):
    976975        setattr(self, opts.fields[i].attname, arg)
    977976
Back to Top