我遇到以下问题。
我有3种不同的型号:
1.)Testsuite(包含测试列表)
2.)测试(具有不同的组,例如“生产”,“现场”,“前端”,“后端”等)
3.)组(所有可用组的列表。
测试人员需要创建一个包含测试列表的测试套件。 但是逐个添加它们并不合适。 更好的选择是按组排序添加它们。
我正在寻找2种解决方案。 在编辑表单中包含过滤器选项。
或其他选项。
编辑表单中的水平列表 能够搜索组标签。
一些代码可以更好地理解。 在forms.py中:
<pre>
class TestSuiteForm(forms.ModelForm):
class Meta:
model = TestSuiteModel
fields = ('name','testcases' , 'nutzer' )
widgets = {
'testcases': autocomplete.ModelSelect2Multiple(
'Testcase_autocomplete'
)
}
class TestCaseForm(forms.ModelForm):
class Meta:
model = TestCaseModel
fields = ('name', 'testsuite' , 'gruppen' , 'portal' )
widgets = {
'testsuite': autocomplete.ModelSelect2Multiple(
'Testsuite_autocomplete'
),
'gruppen': autocomplete.ModelSelect2Multiple(
'Gruppen_autocomplete'
),
}
class GroupForm(forms.ModelForm):
class Meta:
model = GroupModel
fields = ('name', 'testcases' )
widgets = {
'testcases': autocomplete.ModelSelect2Multiple(
'Testcase_autocomplete'
)
}
</pre>
admin.py
<pre>
class TestSuiteFormAdmin(admin.ModelAdmin):
search_fields = ('name',)
form = TestSuiteForm
list_filter = ['name']
class TestCaseAdmin(admin.ModelAdmin):
form = TestCaseForm
list_filter = ['gruppen', ]
list_display = ['name', ]
search_fields = ('name',)
class GroupAdmin(admin.ModelAdmin):
form = GroupForm
list_filter = ['name']
</pre>
和models.py
<pre>
class TestCaseModel(models.Model):
#id = models.CharField(primary_key=True, max_length=50)
name = models.CharField(max_length=50)
gruppen = models.ManyToManyField('GroupModel' , blank=True)
testsuite = models.ManyToManyField('TestSuiteModel' , blank=True)
def __str__(self):
return self.name
class Meta:
db_table = "Testcase"
verbose_name = 'Testcase'
verbose_name_plural = 'Testcases'
@python_2_unicode_compatible
class TestSuiteModel(models.Model):
name = models.CharField(max_length=200)
testcases = models.ManyToManyField('TestCaseModel' , blank=True)
nutzer = models.CharField(max_length=200, blank=True)
def __str__(self):
return self.name
class Meta:
db_table = "Testsuite"
verbose_name = 'Testsuite'
verbose_name_plural = 'Testsuits'
class GroupModel(models.Model):
name = models.CharField(max_length=50)
testcases = models.ManyToManyField('TestCaseModel', blank=True)
def __str__(self):
return self.name
</pre>
答案 0 :(得分:0)
这是我发现的一个解决方案。但这不适用于很多很多领域。
from pprint import pprint
from django.utils.encoding import smart_text
from django.utils.translation import ugettext_lazy as _
class CategoryListFilter(admin.SimpleListFilter):
title = _('TestCaseModel')
parameter_name = 'testcasemodel'
def lookups(self, request, model_admin):
categories = TestCaseModel.objects.all()
for obj in categories:
pprint(vars(obj))
#filter_objects = TestCaseModel.objects.filter(Some_attribut = "some_name")
#filter_objects = TestCaseModel.objects.filter(group = "backend")
def queryset(self, request, queryset):
if self.value():
return queryset.filter(testcasemodel__id=self.value())
class TAdmin(admin.ModelAdmin):
list_filter = (CategoryListFilter,)
对于多对多关系,您将获得无值,并且您无法对此进行过滤。