Skip to content

Instantly share code, notes, and snippets.

@jumbojet
Created December 5, 2013 15:17
Show Gist options
  • Save jumbojet/7807043 to your computer and use it in GitHub Desktop.
Save jumbojet/7807043 to your computer and use it in GitHub Desktop.
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