Hi Martin,

Thanks for the suggestion. I've tried it both ways to no avail.

All,

Here's the code from django.db.models.base.py (in the Model.__init__)
where the trouble seems to start (around line 224):

        # Now we're left with the unprocessed fields that *must* come
from
        # keywords, or default.
        print kwargs
        for field in fields_iter:
            print field.name
            rel_obj = None
            if kwargs:
                if isinstance(field.rel, ManyToOneRel):
                    try:
                        # Assume object instance was passed in.
                        rel_obj = kwargs.pop(field.name)
                    except KeyError:
                        try:
                            # Object instance wasn't passed in -- must
be an ID.
                            val = kwargs.pop(field.attname)
                        except KeyError:
                            val = field.get_default()
                    else:
                        # Object instance was passed in. Special case:
You can
                        # pass in "None" for related objects if it's
allowed.
                        if rel_obj is None and field.null:
                            val = None
                else:
                    val = kwargs.pop(field.attname, field.get_default
())
            else:
                val = field.get_default()
                print field.name + ' default value ' + `val`
            # If we got passed a related instance, set it using the
field.name
            # instead of field.attname (e.g. "user" instead of
"user_id") so
            # that the object gets properly cached (and type checked)
by the
            # RelatedObjectDescriptor.
            if rel_obj:
                setattr(self, field.name, rel_obj)
            else:
                setattr(self, field.attname, val)

If I send in kwargs (and I've confirmed that it's getting what I think
it's getting) it goes merrily along until it gets to my ManyToMany
field at which time, it tries (using the last line above) to call
setattr(self, field.attname, val) with None (the default value for the
field). This triggers a call to the __set__ in related.py which in
turn tries to call the __get__ to find my related object (diligently)
and whammo.

The thing's doing exactly what it's coded to do.

My question is this:

If I'm in the init for this model and the field's default value is
None, why is the code ever trying to go get the related object? The
code should know it's not there. Shouldn't there be a check somewhere
in the stack to prevent the call to related.py in this case (in
__init__ when I don't actually have a related object)?

And why (the HECK) does this work just fine on the command line? That
points to my code, but all my code is doing is generating the kwargs
and feeding it into this process.

Very frustrating...

Thanks for whatever time anyone has on all of this...

Keyton


On Jan 21, 12:12 am, Martin Conte Mac Donell <refl...@gmail.com>
wrote:
> On Wed, Jan 21, 2009 at 2:35 AM, Keyton Weissinger <key...@gmail.com> wrote:
>
> > Oh and just to re-state, this exact same code works like a champ on
> > import of School or Parent data (neither of which have a ManyToMany
> > field).
>
> > Keyton
>
> I can't reproduce this exception. Try to print import_object_dict just
> before "new_object =
> model_import_info.model_for_import.objects.create(**import_object_dict)".
>
> > Note that I am NOT yet trying to SAVE the Student object, just instantiate 
> > it.
>
> Actually Model.objects.create() do try to save() object. If you don't
> want it, call Model(**import_object_dict)
>
> M.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to