The EU can use this form to select the roles (Project Manager, Developer, etc.) that they play for any given project. I want to limit the parameters of the project field only to those who are in the department of employees. Now the EU can choose the project of any department (but not the department, I completely excluded it) How can I do this? queryset = blabla does not work.
MODELS:
class Department(models.Model): name = models.CharField(max_length=20) def __unicode__(self): return self.name class Employee(models.Model): fname = models.CharField(max_length=15) department = models.ForeignKey(Department) def __unicode__(self): return self.fname class Projecttype(models.Model): name = models.CharField(max_length=20) def __unicode__(self): return self.name class Project(models.Model): projecttype = models.ForeignKey(Projecttype) department = models.ForeignKey(Department) members = models.ManyToManyField(Employee, through='Membership') def __unicode__(self): return "%s > %s" % (self.department, self.projecttype) class Role(models.Model): name = models.CharField(max_length=20) def __unicode__(self): return self.name class Membership(models.Model): project = models.ForeignKey(Project, null=True) department = models.ForeignKey(Department) employee = models.ForeignKey(Employee) role = models.ManyToManyField(Role, blank=True, null=True) class Meta: unique_together = (("project", "employee",),)
VIEW:
def employee_edit(request, employee_id): i = get_object_or_404(Employee, pk=employee_id) EmployeeInlineFormSet = inlineformset_factory(Employee, Membership, extra=1, exclude=('department',), queryset=Membership.objects.filter(department=i.department)) if request.method == "POST": f = EmployeeInlineFormSet(request.POST, instance=i) if f.is_valid(): f.save() else: f = EmployeeInlineFormSet(instance=i) return render_to_response('testdb/edit.html', {'item': i, 'formset': f, }, context_instance=RequestContext(request))
JSON: MANAGE.PY DUMPDATA TESTDB --INDENT = 4
[ { "pk": 1, "model": "testdb.department", "fields": { "name": "IT Department" } }, { "pk": 2, "model": "testdb.department", "fields": { "name": "Operations Department" } }, { "pk": 1, "model": "testdb.employee", "fields": { "department": 1, "fname": "Alice" } }, { "pk": 2, "model": "testdb.employee", "fields": { "department": 2, "fname": "Eve" } }, { "pk": 3, "model": "testdb.employee", "fields": { "department": 1, "fname": "Bob" } }, { "pk": 1, "model": "testdb.projecttype", "fields": { "name": "PROCESS IMPROVEMENT" } }, { "pk": 2, "model": "testdb.projecttype", "fields": { "name": "DATA CLEANUP" } }, { "pk": 1, "model": "testdb.project", "fields": { "projecttype": 1, "department": 1 } }, { "pk": 2, "model": "testdb.project", "fields": { "projecttype": 1, "department": 2 } }, { "pk": 3, "model": "testdb.project", "fields": { "projecttype": 2, "department": 1 } }, { "pk": 1, "model": "testdb.role", "fields": { "name": "Project Manager" } }, { "pk": 2, "model": "testdb.role", "fields": { "name": "Analyst" } }, { "pk": 1, "model": "testdb.membership", "fields": { "employee": 1, "department": 1, "project": 1, "role": [ 1, 2 ] } }, { "pk": 2, "model": "testdb.membership", "fields": { "employee": 2, "department": 2, "project": 2, "role": [ 1 ] } }, { "pk": 3, "model": "testdb.membership", "fields": { "employee": 3, "department": 1, "project": 1, "role": [ 1 ] } } ]