Opened 18 years ago

Closed 17 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: dev
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

Description

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_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/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!

Change History (1)

comment:1 by Chris Beaven, 17 years ago

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