python - Django admin view limiting the selection of items from foreign key according to user -
i writing code in django engineering college:
1) college has engineering branches computer engg, mech engg, chemical engg.
2) each branch have different classes frst year enginnering in computer has classes say, fyco1 , fyco2
3) have created different users each head of department(hod) , following models:
class m_branch(models.model): branch_id = models.charfield(max_length=20) branch_name= models.charfield(max_length=50) def __str__(self): return self.branch_name class m_academic_year(models.model): academic_year = models.charfield(max_length=20) def __str__(self): return self.academic_year class m_class(models.model): branch_id = models.foreignkey(m_branch) academic_year = models.foreignkey(m_academic_year) class_id = models.charfield(max_length=20) class_name = models.charfield(max_length=20) def __str__(self): return self.class_name
4) each hod (user) must able add class own department(branch),how achieve functionality???
note: have not written views,(and don't want so). using django's default admin view
to read branch of hod in m_classadmin first make relation between hod , branch this, make changes in model.py as
from django.contrib.auth.models import user
add hod field
hod = models.foreignkey(user)
to store hod , branch relation
now model.py willbe...
class m_branch(models.model): hod = models.foreignkey(user) branch_id = models.charfield(max_length=20) branch_name= models.charfield(max_length=50) def __str__(self): return self.branch_name class m_academic_year(models.model): academic_year = models.charfield(max_length=20) def __str__(self): return self.academic_year class m_class(models.model): branch_id = models.foreignkey(m_branch) academic_year = models.foreignkey(m_academic_year) class_id = models.charfield(max_length=20) class_name = models.charfield(max_length=20) def __str__(self): return self.class_name
then in admin.py override get_queryset , formfield_for_foreignkey
class m_classadmin(admin.modeladmin): def get_queryset(self, request): print("query set") qs = super(m_classadmin, self).get_queryset(request) if request.user.is_superuser: return qs user = user.objects.get(username=request.user.username) branch = m_branch.objects.get(hod = user) print("branch_id_id",branch) return qs.filter(branch_id_id=branch) def formfield_for_foreignkey(self, db_field, request, **kwargs): print("i in func::") if db_field.name == "branch_id": print("db_field.name",db_field.name) user = user.objects.get(username=request.user.username) if not user.is_superuser: print('user=',user) branch = m_branch.objects.get(hod = user) print('branch = ',branch) '''if user=='e0711001': kwargs["queryset"] = m_branch.objects.filter(branch_id='b101') #elif user=='e0711002': else: kwargs["queryset"] = m_branch.objects.filter(branch_id='b102')''' #kwargs["queryset"] = m_branch.objects.filter(branch_id=user.branch.id) kwargs["queryset"] = m_branch.objects.filter(branch_name=branch) return super(m_classadmin, self).formfield_for_foreignkey(db_field, request, **kwargs) fields = ['branch_id','academic_year','class_name'] list_filter = ['academic_year','class_name'] search_fields = ['branch_id','academic_year','class_name'] list_display = ('class_name','branch_id','academic_year') admin.site.register(m_class,m_classadmin)
Comments
Post a Comment