Created
December 19, 2013 15:23
-
-
Save JuniorLima/8040860 to your computer and use it in GitHub Desktop.
Personalizar formulário do admin do Django com base no usuário - Dois exemplos diferentes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyModelAdmin(admin.ModelAdmin): | |
def get_fieldsets(self, request, obj=None): | |
if obj: | |
return [(None, {'fields': ('field_c', 'field_b')})] | |
return [(None, {'fields': ('field_a', 'field_b', 'field_c')})] | |
def get_form(self, request, obj=None, **kwargs): | |
if obj: | |
defaults = {'exclude': ('field_a',)} | |
else: | |
defaults = {} | |
defaults.update(kwargs) | |
return super(MyModelAdmin, self).get_form(request, obj, **defaults) | |
# https://code.djangoproject.com/ticket/8387 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EventForm(forms.ModelForm): | |
class Meta: | |
model = models.Event | |
exclude = ['description',] | |
class EventAdminForm(forms.ModelForm): | |
class Meta: | |
model = models.Event | |
class EventAdmin(admin.ModelAdmin): | |
def get_form(self, request, obj=None, **kwargs): | |
if request.user.is_superuser: | |
return EventAdminForm | |
else: | |
return EventForm | |
admin.site.register(models.Event, EventAdmin) | |
# http://stackoverflow.com/questions/687829/django-overriding-get-form-to-customize-admin-forms-based-on-request |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment