I am trying to get a working setup for improved display of user details in
the Django Admin.

I am working with a legacy database, so all tables have a character-based
"username" (rather than the numeric one which Django uses), which is linked
to the "auth_user" table.

#models

class UserProxy(User):
    class Meta:
        proxy = True
    def __unicode__(self):
        return u'%s %s' % (self.first_name, self.last_name)

class Test(models.Model):
    id = models.AutoField(primary_key=True, db_column='TestID')
    label = models.CharField(max_length=50, null=True, blank=True)
    contact = models.ForeignKey(UserProxy, db_column='ContactUserID',
to_field='username', null=True, blank=True, related_name='contact')

The display of the above Test table in the Django Admin works well - the
user's first and last name show up in the list of records for Test.
However, when I try and add and save a record, I get this error message:

Exception Type: ValueError at /admin/edit/test/add/
Exception Value: Cannot assign "<User: foo>": "Test.contact" must be a
"UserProxy" instance.

Here is the traceback:

File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py"
in get_response
  100.                     response = callback(request, *callback_args,
**callback_kwargs)
File
"/usr/local/lib/python2.6/dist-packages/django/contrib/admin/options.py" in
wrapper
  239.                 return self.admin_site.admin_view(view)(*args,
**kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py" in
_wrapped_view
  76.                     response = view_func(request, *args, **kwargs)
File
"/usr/local/lib/python2.6/dist-packages/django/views/decorators/cache.py" in
_wrapped_view_func
  69.         response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/sites.py"
in inner
  190.             return view(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py" in
_wrapper
  21.             return decorator(bound_func)(*args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py" in
_wrapped_view
  76.                     response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py" in
bound_func
  17.                 return func(self, *args2, **kwargs2)
File "/usr/local/lib/python2.6/dist-packages/django/db/transaction.py" in
_commit_on_success
  299.                     res = func(*args, **kw)
File
"/usr/local/lib/python2.6/dist-packages/django/contrib/admin/options.py" in
add_view
  777.             if form.is_valid():
File "/usr/local/lib/python2.6/dist-packages/django/forms/forms.py" in
is_valid
  121.         return self.is_bound and not bool(self.errors)
File "/usr/local/lib/python2.6/dist-packages/django/forms/forms.py" in
_get_errors
  112.             self.full_clean()
File "/usr/local/lib/python2.6/dist-packages/django/forms/forms.py" in
full_clean
  269.         self._post_clean()
File "/usr/local/lib/python2.6/dist-packages/django/forms/models.py" in
_post_clean
  316.         self.instance = construct_instance(self, self.instance,
opts.fields, opts.exclude)
File "/usr/local/lib/python2.6/dist-packages/django/forms/models.py" in
construct_instance
  51.             f.save_form_data(instance, cleaned_data[f.name])
File
"/usr/local/lib/python2.6/dist-packages/django/db/models/fields/__init__.py"
in save_form_data
  416.         setattr(instance, self.name, data)
File
"/usr/local/lib/python2.6/dist-packages/django/db/models/fields/related.py"
in __set__
  318.                                  self.field.name,
self.field.rel.to._meta.object_name))

If I switch back to using:
    contact = models.ForeignKey(User, ...
then editing works OK, but the list now just displays the (ugly) username.

Any help or suggestions appreciated.

Thanks,
Derek

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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