Re: Django admin - how to hide some fields in User edit?

2011-07-06 Thread Michał Sawicz
Dnia 2011-07-06, śro o godzinie 14:40 -0700, galgal pisze:
> Full code:
> class UserProfileAdmin(UserAdmin):
> inlines = [UserProfileInline,]
> list_filter = ('userprofile__type','userprofile__cities',)
> search_fields = ['userprofile__type', 'username',
> 'userprofile__cities__name', 'email', 'first_name', 'last_name',]

Use tuples instead of lists, they're cheaper. And lists don't require
the trailing comma.

--8<--

> def get_fieldsets(self, request, obj=None):
> if obj:
> if request.user.id == 1:
> return self.declared_fieldsets
> else:
> if obj.get_profile().type==1:
> return (
> (None, {'fields': ('username', 'password')}),
> (_('Personal info'), {'fields': ('first_name',
> 'last_name', 'email')}),
> (_('Important dates'), {'fields':
> ('last_login', 'date_joined')}),
> )
> else:
> return (
> (None, {'fields': ('username', 'password')}),
> (_('Personal info'), {'fields': ('first_name',
> 'last_name', 'email')}),
> (_('Permissions'), {'fields': ('is_active',
> 'is_staff', 'user_permissions')}),
> (_('Important dates'), {'fields':
> ('last_login', 'date_joined')}),
> )
> else:
> return self.add_fieldsets
Why not using super().get_fieldsets() when there is no object or your
user isn't 1?

> def get_form(self, request, obj=None, **kwargs):
> if obj:
> if obj.get_profile().type==1:
> self.exclude = ('user_permissions',)
> return super(UserProfileAdmin, self).get_form(request,
> obj=None, **kwargs)
Should be obj=obj, not obj=None, you're always getting a new user form.

-- 
Michał (Saviq) Sawicz 


signature.asc
Description: This is a digitally signed message part


Odp: Re: Django admin - how to hide some fields in User edit?

2011-07-06 Thread galgal
Environment:


Request Method: GET
Request URL: http://localhost:8000/admin/auth/user/2/

Django Version: 1.3
Python Version: 2.7.1
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'debug_toolbar',
 'templatelibrary',
 'apps.index',
 'apps.city']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Template error:
In template 
c:\python27\lib\site-packages\django-1.3-py2.7.egg\django\contrib\admin\templates\admin\includes\fieldset.html,
 
error at line 6
   Caught KeyError while rendering: "Key 'password' not found in Form"
   1 : 


   2 : {% if fieldset.name %}{{ fieldset.name }}{% endif %}


   3 : {% if fieldset.description %}


   4 : {{ fieldset.description|safe 
}}


   5 : {% endif %}


   6 :  {% for line in fieldset %} 


   7 : 


   8 : {% if line.fields|length_is:'1' %}{{ line.errors }}{% 
endif %}


   9 : {% for field in line %}


   10 : 


   11 : {% if not line.fields|length_is:'1' and not 
field.is_readonly %}{{ field.errors }}{% endif %}


   12 : {% if field.is_checkbox %}


   13 : {{ field.field }}{{ field.label_tag }}


   14 : {% else %}


   15 : {{ field.label_tag }}


   16 : {% if field.is_readonly %}


Traceback:
File 
"C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\core\handlers\base.py"
 
in get_response
  111. response = callback(request, *callback_args, 
**callback_kwargs)
File 
"C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\contrib\admin\options.py"
 
in wrapper
  307. return self.admin_site.admin_view(view)(*args, 
**kwargs)
File 
"C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\utils\decorators.py" 
in _wrapped_view
  93. response = view_func(request, *args, **kwargs)
File 
"C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\views\decorators\cache.py"
 
in _wrapped_view_func
  79. response = view_func(request, *args, **kwargs)
File 
"C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\contrib\admin\sites.py"
 
in inner
  197. return view(request, *args, **kwargs)
File 
"C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\utils\decorators.py" 
in _wrapper
  28. return bound_func(*args, **kwargs)
File 
"C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\utils\decorators.py" 
in _wrapped_view
  93. response = view_func(request, *args, **kwargs)
File 
"C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\utils\decorators.py" 
in bound_func
  24. return func(self, *args2, **kwargs2)
File 
"C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\db\transaction.py" 
in inner
  217. res = func(*args, **kwargs)
File 
"C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\contrib\admin\options.py"
 
in change_view
  1030. return self.render_change_form(request, context, 
change=True, obj=obj)
File 
"C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\contrib\admin\options.py"
 
in render_change_form
  708. ], context, context_instance=context_instance)
File 
"C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\shortcuts\__init__.py"
 
in render_to_response
  20. return HttpResponse(loader.render_to_string(*args, **kwargs), 
**httpresponse_kwargs)
File 
"C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\template\loader.py" 
in render_to_string
  188. return t.render(context_instance)
File 
"C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\template\base.py" 
in render
  123. return self._render(context)
File 
"C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\template\base.py" 
in _render
  117. return self.nodelist.render(context)
File 
"C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\template\base.py" 
in render
  744. bits.append(self.render_node(node, context))
File 
"C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\template\debug.py" 
in render_node
  73. result = node.render(context)
File 
"C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\template\loader_tags.py"
 
in render
  127. return compiled_parent._render(context)
File 
"C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\template\base.py" 
in _render
  117. return self.nodelist.render(context)
File 
"C:\Python27\lib\site-packages\django-1.3-py2.7.egg

Re: Django admin - how to hide some fields in User edit?

2011-07-06 Thread Michał Sawicz
Dnia 2011-07-06, śro o godzinie 13:16 -0700, galgal pisze:
> Any idea how to overwrite it dynamically?
> def get_form(self, request, obj=None, **kwargs):
> self.exclude = ('user_permissions',)
> return super(UserProfileAdmin, self).get_form(request,
> obj=None, **kwargs)
> Throws:
> TemplateSyntaxError at /admin/auth/user/2/
> Caught KeyError while rendering: "Key 'password' not found in Form"

That exception is irrelevant to the code you provided. Paste full code
and full backtrace.

-- 
Michał (Saviq) Sawicz 


signature.asc
Description: This is a digitally signed message part


Odp: Re: Django admin - how to hide some fields in User edit?

2011-07-06 Thread galgal
Any idea how to overwrite it dynamically?
def get_form(self, request, obj=None, **kwargs):
self.exclude = ('user_permissions',)
return super(UserProfileAdmin, self).get_form(request, obj=None, 
**kwargs)
Throws:
TemplateSyntaxError at /admin/auth/user/2/

Caught KeyError while rendering: "Key 'password' not found in Form"

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/771t3uo6qbgJ.
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.



Re: Django admin - how to hide some fields in User edit?

2011-07-06 Thread Michał Sawicz
Dnia 2011-07-06, śro o godzinie 00:31 -0700, galgal pisze:
> Ok but like I said in previous post, exclude = ('groups',) etc doesn't
> work :/ 

It does, you just need to override both exclude / fields _and_
fieldsets.

The exception you've mentioned resulted from the fact that you've
excluded the field from the form, but the fieldsets still defined it as
one of the elements, so the form failed.  
-- 
Michał (Saviq) Sawicz 


signature.asc
Description: This is a digitally signed message part


Odp: Re: Django admin - how to hide some fields in User edit?

2011-07-06 Thread galgal
Ok but like I said in previous post, exclude = ('groups',) etc doesn't work 
:/

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/-exygvWEN9UJ.
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.



Re: Django admin - how to hide some fields in User edit?

2011-07-05 Thread Michał Sawicz
Dnia 2011-07-05, wto o godzinie 15:13 -0700, galgal pisze:
> I made it in that way, and it works:
Remember that you need to exclude the field from the form, too, if you
don't want malicious attempts to inject the groups to work. 
-- 
Michał (Saviq) Sawicz 


signature.asc
Description: This is a digitally signed message part


Odp: Re: Odp: Re: Django admin - how to hide some fields in User edit?

2011-07-05 Thread galgal
I made it in that way, and it works:
def get_fieldsets(self, request, obj=None):
if obj:
if request.user.id == 1:
return self.declared_fieldsets
else:
if obj.get_profile().type==1:
return (
(None, {'fields': ('username', 'password')}),
(_('Personal info'), {'fields': ('first_name', 
'last_name', 'email')}),
(_('Important dates'), {'fields': ('last_login', 
'date_joined')}),
)
else:
return (
(None, {'fields': ('username', 'password')}),
(_('Personal info'), {'fields': ('first_name', 
'last_name', 'email')}),
(_('Permissions'), {'fields': ('is_active', 
'is_staff', 'user_permissions')}),
(_('Important dates'), {'fields': ('last_login', 
'date_joined')}),
)
else:
return self.add_fieldsets

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/Mq--t4_P1qMJ.
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.



Re: Odp: Re: Django admin - how to hide some fields in User edit?

2011-07-04 Thread Michał Sawicz
Dnia 2011-07-04, pon o godzinie 15:20 -0700, galgal pisze:
> Yes I read it.  exclude = ('groups',) throws: 
> Caught KeyError while rendering: "Key 'groups' not found in Form"

That's because the stock UserAdmin defines fieldsets in which 'groups'
is a field, you need to modify the fieldsets, too. [1] 

[1]
https://code.djangoproject.com/browser/django/trunk/django/contrib/auth/admin.py#L27
-- 
Michał (Saviq) Sawicz 


signature.asc
Description: This is a digitally signed message part


Odp: Re: Django admin - how to hide some fields in User edit?

2011-07-04 Thread galgal
Yes I read it.  exclude = ('groups',) throws:

Caught KeyError while rendering: "Key 'groups' not found in Form"

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/V5P1M_GH80sJ.
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.



Re: Django admin - how to hide some fields in User edit?

2011-07-04 Thread Michał Sawicz
Dnia 2011-07-04, pon o godzinie 15:13 -0700, galgal pisze:
> That's my code now - how to hide groups for example?

Have you tried reading the Django Admin docs? [1] [2]

[1] https://docs.djangoproject.com/en/1.3/ref/contrib/admin/
[2]
https://docs.djangoproject.com/en/1.3/ref/contrib/admin/#django.contrib.admin.ModelAdmin.exclude

P.S. please fix your e-mail client not to translate "Re:" in responses'
subjects.
-- 
Michał (Saviq) Sawicz 


signature.asc
Description: This is a digitally signed message part


Odp: Re: Odp: Re: Django admin - how to hide some fields in User edit?

2011-07-04 Thread galgal
That's my code now - how to hide groups for example?

admin.site.unregister(User)
class UserProfileInline(admin.StackedInline):
model = UserProfile
filter_horizontal = ('cities',)
extra = 1
max_num = 1
filter_horizontal = ('cities',)

class UserProfileAdmin(UserAdmin):
inlines = [UserProfileInline,]
list_filter = ('userprofile__type','userprofile__cities',)
search_fields = ['userprofile__type', 'username', 
'userprofile__cities__name', 'email', 'first_name', 'last_name',]

# show users list - if user_id=1 show all, else: show id's > 1
def queryset(self, request):
qs = super(UserProfileAdmin, self).queryset(request)
if request.user.id == 1:
return qs
return qs.filter(id__gt=1)

admin.site.register(User, UserProfileAdmin)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/ZzrytaO1EOcJ.
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.



Re: Odp: Re: Django admin - how to hide some fields in User edit?

2011-07-04 Thread Michał Sawicz
Dnia 2011-07-04, pon o godzinie 14:19 -0700, galgal pisze:
> I'm rather searchung the way to do that in my admin.py file not in
> core files. 

Obviously the proposal was... fundamentally wrong.

Create your own UserAdmin, unregister the one registered by
django.contrib.admin and register your own.

> admin.site.unregister(User)
> admin.site.register(User, MyUserAdmin)
-- 
Michał (Saviq) Sawicz 


signature.asc
Description: This is a digitally signed message part


Odp: Re: Django admin - how to hide some fields in User edit?

2011-07-04 Thread galgal
I'm rather searchung the way to do that in my admin.py file not in core 
files.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/sZ83LDHBIC8J.
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.



Re: Django admin - how to hide some fields in User edit?

2011-07-04 Thread leo
try to modify this file 
"Python26\Lib\site-packages\django\contrib\auth\admin.py"


find this line
(_('Groups'), {'fields': ('groups',)}),

comment this line you will remove groups

On 2011-7-5 4:59, galgal wrote:
How can I hide fields in admin User edit? Mainly I want to hide 
permissions and groups selecting in some exceptions, but exclude 
variable doesn't work :/

--
You received this message because you are subscribed to the Google 
Groups "Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/ydsmKWUyXQMJ.

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.


--
chlin

--
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.



Django admin - how to hide some fields in User edit?

2011-07-04 Thread galgal
How can I hide fields in admin User edit? Mainly I want to hide permissions 
and groups selecting in some exceptions, but exclude variable doesn't work 
:/

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/ydsmKWUyXQMJ.
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.