응용 프로그램 및 역할에 대한 모델이 있습니다. 역할은 FK Role_type에 연결됩니다.이 Role_type은 FK에 의해 Role_types를 사용할 수있는 응용 프로그램에 연결됩니다 (이 질문은 지나치게 단순화되었지만 충분하다고 생각합니다). 새로운 응용 프로그램을 만들 수있는 양식을 만들고 관련된 역할을 사람들에게 할당하는 레코드를 만들 수있는 방법이 필요합니다. (공백으로 둘 수 있습니다.)여러 개의 연결된 장고 양식, 하나의 제출
응용 프로그램의 양식을 만드는 동안 관련 역할 유형이 페이지에 표시되고 사용자에게 채워지는 드롭 다운이 있습니다. 하지만 타격 제출은 관련된 역할 레코드를 만들지 않았습니다. 모든 연구가 인라인 모델 양식으로 계속 돌아가는 것 같지만 문서는 실제로 나에게 의미가 없습니다. 예제의 입력 내용은 필자가 필요로하는 것과 상관 관계가없는 것처럼 보입니다.
나는 이것이 중복처럼 보일지도 모른다는 것을 알고있다. 그러나 나는 내가 이것과 관련있는 것처럼 보이는 모든 질문을 보았다고 말할 때 나를 신뢰한다!
편집 : 내 POST는 다음과 같습니다 : QueryDict: {u'roles-MAX_NUM_FORMS': [u'1000'], u'roles-1-role_type': [u'4'], u'roles-0-user': [u'1'], u'app-owner': [u'1'], u'app-name': [u'1234'], u'app-serviceTier': [u''], u'app-jiraProject': [u''], u'roles-TOTAL_FORMS': [u'2'], u'roles-1-user': [u''], u'roles-0-role_type': [u'3'], u'csrfmiddlewaretoken': [u'eGsDwtsSQJfl0'], u'roles-INITIAL_FORMS': [u'2']}>
. 인쇄 RolesFormSet 나에게 동일한 출력을 제공
models.py (아래에 의견을 참조)
class Item(models.model):
name = models.CharField(max_length=255)
roles = models.ManyToManyField(User, through='Role')
class Application(Item):
other_assorted_attributes = foo
class RoleType(models.Model):
name = models.CharField(max_length=255)
class ItemTypeRoleMapping(models.Model):
''' pairs role-types (e.g., Developer) with an Item class they are relevant to'''
roleType = models.ForeignKey(RoleType)
itemType = models.CharField(max_length=255, choices=itemChoices)
class Role(models.Model):
role_type = models.ForeignKey(RoleType)
user = models.ForeignKey(User)
item = models.ForeignKey(Item)
views.py
def buildRolesFormset(itemClass):
''' given an item, build a form for all associated roles '''
roleTypesForItem = ItemTypeRoleMapping.objects.all().filter(itemType=itemClass.__name__)
applicable_roles = [{'role_type': roleType} for roleType in roleTypesForItem]
# formset = rolesFormSet(initial=initial, prefix='roles')
RoleFormSet = inlineformset_factory(Application, Role, extra=len(roleTypesForItem), can_delete=False)
formset = RoleFormSet()
for subform, data in zip(formset.forms, applicable_roles):
subform.initial = data
return formset
def new(request):
''' Create a new application '''
user = request.user
# check permission
if request.method == 'POST':
appform = AppForm(request.POST, prefix='app')
if appform.is_valid():
app = appform.save(commit=False)
rolesInlineFormSet = inlineformset_factory(Application, Role)
# pdb.set_trace()
rolesFormSet = rolesInlineFormSet(request.POST, instance=app, prefix='roles')
if rolesFormSet.is_valid():
rolesFormSet.save()
else:
print rolesFormSet.errors
app = appform.save()
# check rolesFormSet
return redirect(reverse('index'))
else:
appform = AppForm(prefix='app')
rolesFormSet = buildRolesFormset(Application)
return render(request, 'who/editapp.html',
{'appform': appform,
'rolesFormSet': rolesFormSet
})
내 rolesFormSet이 is_valid를 지우지 않고 그 이유를 알 수 없습니다. pdb에서 rolesFormSet.data를 pring 할 때'' –
thumbtackthief
','u'rols-0-role_type ' 왜 저장하지 않는지 잘 모르겠다. 'app'접두사가 붙은 정보가 왜 있는지 혼란 스럽다. FWIW, POST 요청을 인쇄하면 정확한 결과를 얻을 수 있습니다. – thumbtackthief
뷰에서'print rolesFormset.errors'를 실행하면 왜 formset이 유효하지 않은지 알 수 있습니다. – Greg