﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
1272	KeyError when using users.AddManipulator() and have a model with meta.ForeignKey(auth.User)	berto	Adrian Holovaty	"I have a model that has a !ForeignKey to an auth.User object:

{{{
class Person(meta.Model):
    user = meta.ForeignKey(auth.User, edit_inline=meta.STACKED,
                           num_in_admin=1, max_num_in_admin=1, core=True)
    address = meta.ForeignKey(Address, null=True, blank=True)
    phone = meta.PhoneNumberField(null=True, blank=True)
    dob = meta.DateField(blank=True, null=True)

    def __repr__(self):
        u = self.get_user()
        
        return ' '.join((u.first_name, u.last_name))

    class META:
        verbose_name = _('Person')
        verbose_name_plural = _('People')
        admin = meta.Admin()
}}}

When I try to use an !AddManipulator to add a user like this:

{{{
    personal_info = request.POST.copy()
    personal_info['username'] = 'somebody'
    personal_info['is_staff'] = False
    personal_info['is_active'] = False
    personal_info['is_superuser'] = False
    
    username_counter = 0

    user_manip = users.AddManipulator()
    #user_manip.do_html2python(personal_info)
    
    while(1):
        try:
            u = user_manip.save(personal_info)
            break
}}}

This causes the following Exception to be raised:

{{{

KeyError at /sign_up/save/
'person'
Request Method: 	GET
Request URL: 	http://localhost:8000/sign_up/save/
Exception Type: 	KeyError
Exception Value: 	'person'
Exception Location: 	/Users/berto/Development/django/django/core/meta/__init__.py in manipulator_save, line 1809
Traceback (innermost last)
[...]
}}}

I tried setting personal_info['person'] = None, but I then get yet another error:

{{{
AttributeError at /sign_up/save/
'list' object has no attribute 'items'
}}}

To solve this problem, I added the following bit of foo to django/core/meta/__init__.py:

{{{
[berto@peroxi][659]$ svn diff django/core/meta/__init__.py
Index: django/core/meta/__init__.py
===================================================================
--- django/core/meta/__init__.py        (revision 2116)
+++ django/core/meta/__init__.py        (working copy)
@@ -1807,7 +1807,10 @@
         child_follow = self.follow.get(related.name, None)
 
         if child_follow:
-            obj_list = expanded_data[related.var_name].items()
+            try:
+                obj_list = expanded_data[related.var_name].items()
+            except: continue
+            
             obj_list.sort(lambda x, y: cmp(int(x[0]), int(y[0])))
 
             # For each related item...
}}}

I'm not sure if it's exactly proper to simply ignore the exception, but I've not run into any adverse affects doing so.

Oh, and as a side comment, the line:

{{{
user_manip.do_html2python(personal_info)
}}}

causes django to just hang for some reason (which is why I commented it out).

Thanks!"	defect	closed	Core (Other)	dev	normal	invalid			Unreviewed	0	0	0	0	0	0
