Hi,

I'm not sure what's causing your error, but it appears that using
User.add_to_class is not the recommended approach for extending the
user model.

James Bennet explains it very well here -
http://groups.google.com/group/django-users/browse_thread/thread/a35d94ce1a7893b0/9e45e7bfc58cfa05?lnk=gst&q=add_to_class#9e45e7bfc58cfa05

Basically, create a UserProfile class and associate it to your user
via the User.profile property. There are many resources online, just
search for 'django user.profile'

I use and prefer this approach
http://www.codekoala.com/blog/2009/quick-django-tip-user-profiles/

Good luck,
Sam


On Nov 1, 3:26 am, ewoudenberg <eric.woudenb...@gmail.com> wrote:
> Hi,
>
> I'm adding row level permissions to the admin using this 
> guideline:http://www.djangosnippets.org/snippets/1054/. Everything works fine
> when I add a simple ForeignKey to the User object and use that to
> identify records 'owned' by the user. e.g.:
>
>   from django.contrib.auth.models import User
>   User.add_to_class('venue', models.ForeignKey(Venue))
>
> However, when I add a ManyToManyField, so that a user can effectively
> be many owners, e.g.:
>
>   from django.contrib.auth.models import User
>   User.add_to_class('venues', models.ManyToManyField(Venue))
>
> I get this Django crash:
>
> Exception Value:
> Cannot resolve keyword 'user' into field. Choices are:  <shows all
> fields of the Venue object>
>
> Exception Location:     C:\Python25\lib\site-packages\django-trunk\django
> \db\models\sql\query.py in setup_joins, line 1737
>
> Here is the offending line:
>
>         print request.user.venues.all()
>
> However, this same operation works fine from the command line:
>
> >>> from django.db import models
> >>> from django.contrib.auth.models import User
> >>> from dharmaseed.talks.models import Venue
> >>> User.add_to_class('venues', models.ManyToManyField(Venue))
> >>> dennis = User.objects.get(id=7)
> >>> dennis.venues.count()
> 2
> >>> dennis.venues.all()
>
> [<Venue: Insight Meditation Society - Forest Refuge>, <Venue: Insight
> Meditation Society - Retreat Center>]
>
> Is this a Django bug? What could be different about request.user
> during a web fetch and the user instance I get from the command line?
> Should I just go back to ForeignKey ownership instead of ManyToMany?
>
> Any help or hints would be greatly appreciated!
>
> Thank you,
> Eric Woudenberg
>
> Trace:
>
> Environment:
>
> Request Method: GET
> Request URL:http://127.0.0.1:8000/admin/talks/talk/
> Django Version: 1.1 rc 1
> Python Version: 2.5.2
> Installed Applications:
> ['django.contrib.admin',
>  'django.contrib.auth',
>  'django.contrib.contenttypes',
>  'django.contrib.sessions',
>  'django.contrib.sites',
>  'django.contrib.flatpages',
>  'django.contrib.redirects',
>  'dharmaseed.talks',
>  'dharmaseed.quicklinks',
>  'zilbo.accounts']
> Installed Middleware:
> ('django.middleware.common.CommonMiddleware',
>  'django.contrib.sessions.middleware.SessionMiddleware',
>  'django.contrib.auth.middleware.AuthenticationMiddleware',
>  'django.middleware.doc.XViewMiddleware',
>  'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
>  'django.contrib.redirects.middleware.RedirectFallbackMiddleware')
>
> Traceback:
> File "C:\Python25\lib\site-packages\django-trunk\django\core\handlers
> \base.py" in get_response
>   92.                 response = callback(request, *callback_args,
> **callback_kwargs)
> File "C:\Python25\lib\site-packages\django-trunk\django\contrib\admin
> \sites.py" in root
>   490.                 return self.model_page(request, *url.split('/',
> 2))
> File "C:\Python25\lib\site-packages\django-trunk\django\views
> \decorators\cache.py" in _wrapped_view_func
>   44.         response = view_func(request, *args, **kwargs)
> File "C:\Python25\lib\site-packages\django-trunk\django\contrib\admin
> \sites.py" in model_page
>   509.         return admin_obj(request, rest_of_url)
> File "C:\Python25\lib\site-packages\django-trunk\django\contrib\admin
> \options.py" in __call__
>   1090.             return self.changelist_view(request)
> File "C:\Python25\lib\site-packages\django-trunk\django\contrib\admin
> \options.py" in changelist_view
>   897.                 self.date_hierarchy, self.search_fields,
> self.list_select_related, self.list_per_page, self.list_editable,
> self)
> File "C:\Python25\lib\site-packages\django-trunk\django\contrib\admin
> \views\main.py" in __init__
>   39.         self.root_query_set = model_admin.queryset(request)
> File "..\dharmaseed\talks\admin.py" in queryset
>   12.         return self.filter_user_queryset(queryset, request)
> File "..\dharmaseed\talks\admin.py" in filter_user_queryset
>   110.         print request.user.venues.all()
> File "C:\Python25\lib\site-packages\django-trunk\django\db\models
> \manager.py" in all
>   105.         return self.get_query_set()
> File "C:\Python25\lib\site-packages\django-trunk\django\db\models
> \fields\related.py" in get_query_set
>   424.             return superclass.get_query_set
> (self)._next_is_sticky().filter(**(self.core_filters))
> File "C:\Python25\lib\site-packages\django-trunk\django\db\models
> \query.py" in filter
>   498.         return self._filter_or_exclude(False, *args, **kwargs)
> File "C:\Python25\lib\site-packages\django-trunk\django\db\models
> \query.py" in _filter_or_exclude
>   516.             clone.query.add_q(Q(*args, **kwargs))
> File "C:\Python25\lib\site-packages\django-trunk\django\db\models\sql
> \query.py" in add_q
>   1675.                             can_reuse=used_aliases)
> File "C:\Python25\lib\site-packages\django-trunk\django\db\models\sql
> \query.py" in add_filter
>   1569.                     negate=negate,
> process_extras=process_extras)
> File "C:\Python25\lib\site-packages\django-trunk\django\db\models\sql
> \query.py" in setup_joins
>   1737.                             "Choices are: %s" % (name, ",
> ".join(names)))
>
> Exception Type: FieldError at /admin/talks/talk/
> Exception Value: Cannot resolve keyword 'user' into field. Choices
> are: audio_header, contact_email, description, id, location, name,
> retreat, rssfeed_author, rssfeed_description, rssfeed_image_url,
> rssfeed_itunes_image_url, rssfeed_keywords, rssfeed_link,
> rssfeed_title, talk, venue_code, website
--~--~---------~--~----~------------~-------~--~----~
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