models.py
class Keyword(models.Model):
keyname = models.CharField(max_length=45, unique = True)
project = models.ForeignKey('Project')
class KeywordCache(models.Model):
keyword = models.ForeignKey('Keyword')
testcase = models.ForeignKey('TestcaseCache')
class TestcaseCache(models.Model):
name = models.CharField(max_length=60)
package_name = models.CharField(max_length=100)
summary = models.TextField()
pre_requisite = models.TextField(blank = True)
steps = models.TextField()
expected_results = models.TextField()
post_test = models.TextField(blank = True)
data_provider = models.TextField(blank = True)
author = models.ForeignKey(Engineer, related_name = 'author_id')
reviewer = models.ForeignKey(Engineer,related_name = 'reviewer_id',null=True,blank = True,default=None)
release = models.CharField(max_length=45, blank = True)
creation_date = models.DateField(auto_now_add=True)
bugs = models.TextField(blank = True)
notes = models.TextField(blank = True)
project = models.ForeignKey('Project')
feature = models.ForeignKey('FeatureCache')
Forms.py
from django import forms
from apps.tcg.models import TestcaseCache
from apps.tcg.models import Keyword
from django.core.exceptions import ValidationError
from django.forms import ModelForm
# Create the form class.
class TestcaseCacheForm(forms.ModelForm),KeywordForm(models.ModelForm):
class Meta:
model = TestcaseCache,Keyword
fields = ('name','package_name','summary','pre_requisite','steps','expected_results','post_test','data_provider','author','reviewer','release',keyname,'bugs','notes','project','feature')
views.py
from forms import TestcaseCacheForm
def createForm(request):
return render(request,'tcgform.html',{'form':TestcaseCacheForm(),'testcase_object_array':testcase_object_array,'author_name':author_name,'project_name':project_id,'feature_name':feature_name})
答案 0 :(得分:1)
ModelForms不允许您继承多个模型类,但是,视图对它可以处理的表单类的数量无关紧要。对于最终用户,您可以在一个HTML表单中提供两个表单类。例如,假设您有两个模型表单类:
def my_view(request):
keyword_form = KeywordForm(request.POST or None)
another_form = AnotherForm(request.POST or None)
if keyword_form.is_valid and another_form.is_valid():
# do whatever
return render(request, 'your-template.html',
{'keyword_form': keyword_form, 'another_form': another_form})
# your_template.html
<form action="." method="post" enctype="application/x-www-form-urlencoded">
<ul>
{{ keyword_form.as_ul }}
{{ another_form.as_ul }}
</ul>
<button type="submit">Submit</button>
</form>
当然,如果您需要严格控制哪些字段放在哪里,您始终可以定义字段位置,而不是使用快速和脏的渲染。希望能帮到你。