欧盟可以使用此表单选择他们为任何给定项目播放的角色(项目经理,开发人员等)。我想将项目字段选项限制为仅限于员工部门的选项。现在,欧盟可以选择任何部门的项目(但不是部门,我完全排除了)我该怎么做? queryset = blabla不起作用..
模型:
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",),)
查看:
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
]
}
}
]
答案 0 :(得分:8)
此时,您正在创建formset类,请不要在此处传递查询集:
EmployeeInlineFormSet = inlineformset_factory(Employee, Membership, extra=1, exclude=('department',))
在实例化formsete时传递queryset:
f = EmployeeInlineFormSet(instance=i, queryset=Membership.objects.filter(department=i.department))
e.g:
def employee_edit(request, employee_id):
i = get_object_or_404(Employee, pk=employee_id)
queryset=Membership.objects.filter(department=i.department)
EmployeeInlineFormSet = inlineformset_factory(Employee, Membership, extra=1, exclude=('department',))
if request.method == "POST":
f = EmployeeInlineFormSet(request.POST, instance=i, queryset=queryset)
if f.is_valid():
f.save()
else:
f = EmployeeInlineFormSet(instance=i, queryset=queryset)
return render_to_response('testdb/edit.html', {'item': i, 'formset': f, }, context_instance=RequestContext(request))