我正在Django开展维护项目。以下是一个类,它创建一个表单,用于在应用程序中添加业务。
class BusinessForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(BusinessForm, self).__init__(*args, **kwargs)
self.fields['trainers'].widget.attrs['class'] = 'select-menu fl'
name = forms.CharField(required=True,max_length=70, error_messages={'required': 'Please enter the business title'},widget=forms.TextInput({'placeholder':_('Untitled Business'),'autocomplete':'off','onkeyup':'string_to_slug(this.value)'}))
slug = forms.CharField(required=True,max_length=70, error_messages={'required': 'Please enter the business slug'},widget=forms.TextInput({'class':'default-url tttxt-w','style':'width:152px;','title':_('Business Slug'),'autocomplete':'off','style':'padding: 0pt; width: 237px;','onkeyup':'string_to_slug(this.value)'}))
description = forms.CharField(required=False,max_length=5000,widget=forms.Textarea({'class':'textField long'}),error_messages={'required': 'Please enter the description'})
trainers = forms.ModelMultipleChoiceField(required=False,
queryset=Business.objects.filter(
Q(categories__parent_cat__name='Trainers') | Q(categories__name='Trainers') | Q(categories__parent_cat__parent_cat__name='Trainers')).distinct()
)
achievements = forms.CharField(required=False,max_length=5000,widget=forms.Textarea({'class':'textField long' ,'style':'width:567px;','class':'textField long'}),label='Achievements/Certificates')
years_of_experience = forms.FloatField(required=False)
price = forms.FloatField(required=False)
问题在于表单中的Trainers字段。我正在使用以下方法在模板中创建Trainers字段:
<div class="field select-field">
<div class="label">
<label>
{% trans 'Trainers' %}
</label>
</div>
<div class="value">
{{form.trainers}}
</div>
</div>
此Trainers字段显示奇怪的行为,它在我重新启动apache服务器一段时间后填充数据,然后在某个时间后开始显示空白下拉列表。然后再次重启服务器时,它会填充数据。可能的问题是什么?任何指针都将受到高度赞赏。
编辑:我正在使用PostgreSQL数据库,我仍然面临这个问题。不知道是什么导致了这种奇怪的行为。编辑2 :
可以查询:
queryset=Business.objects.filter(
Q(categories__parent_cat__name='Trainers') | Q(categories__name='Trainers') |Q(categories__parent_cat__parent_cat__name='Trainers')).distinct()
可能会导致这个问题(虽然我无法找到任何问题)?