Opened 14 years ago

Closed 13 years ago

#1272 closed defect (invalid)

KeyError when using users.AddManipulator() and have a model with meta.ForeignKey(auth.User)

Reported by: berto Owned by: Adrian Holovaty
Component: Core (Other) Version: master
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


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()
            u =

This causes the following Exception to be raised:

KeyError at /sign_up/save/
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/ in manipulator_save, line 1809
Traceback (innermost last)

I tried setting personal_infoperson? = 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/

[berto@peroxi][659]$ svn diff django/core/meta/
Index: django/core/meta/
--- django/core/meta/        (revision 2116)
+++ django/core/meta/        (working copy)
@@ -1807,7 +1807,10 @@
         child_follow = self.follow.get(, 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:


causes django to just hang for some reason (which is why I commented it out).


Change History (1)

comment:1 Changed 13 years ago by Chris Beaven

Resolution: invalid
Status: newclosed

This will be obsolete by newforms.

If anyone really needs this fixed (or wants to write a patch), then reopen.

Note: See TracTickets for help on using tickets.
Back to Top