Created
December 5, 2013 15:17
-
-
Save jumbojet/7807043 to your computer and use it in GitHub Desktop.
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 DepartmentAdmin(admin.ModelAdmin): | |
def formfield_for_foreignkey(self, db_field, request, **kwargs): | |
if db_field.name == "employee": | |
''' The idea here is to read the string in request path as when django edits a field it has its key at the end of the string after add''' | |
request_path_array = filter(None,request.path.split('/')) | |
if request_path_array[len(request_path_array)-1] !="add": | |
'''Now get all the employee with particular department id''' | |
kwargs["queryset"] = Employee.objects.filter(departmentid=request_path_array[len(request_path_array)-1]) | |
return db_field.formfield(**kwargs) | |
return super(DepartmentAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment