Skip to content

Instantly share code, notes, and snippets.

@faisalmahmud
Forked from ragsagar/admin.py
Created March 8, 2022 18:12
Show Gist options
  • Save faisalmahmud/8df6cc404ca01a27bd7e1d5a687a3863 to your computer and use it in GitHub Desktop.
Save faisalmahmud/8df6cc404ca01a27bd7e1d5a687a3863 to your computer and use it in GitHub Desktop.
Making a field in a django admin form visible or invisible depending on the user
from django.contrib import admin
from books.models import Agent, PolicyIssue
class PolicyIssueAdmin(admin.ModelAdmin):
def add_view(self, request, form_url='', extra_context=None):
if request.user.get_profile().is_employee:
self.model.branch.field.editable = False
else:
self.model.branch.field.editable = True
return super(PolicyIssueAdmin, self).add_view(request, form_url)
admin.site.register(PolicyIssue, PolicyIssueAdmin)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment