当我使用CreateView创建乐谱时,我想使用order_by('username')进行查询,但是用户名不是英文,大多数是中文拼音,我想对它们进行排序,我粘贴了部分型号,视图和表格如下,感谢您的建议和帮助。
class Score(models.Model):
test = models.ForeignKey(Test, verbose_name="考试名称", null=True, blank=True, on_delete=models.CASCADE)
user = models.ForeignKey(User, verbose_name="应考人", null=True, blank=True, on_delete=models.CASCADE)
score = models.FloatField(default=0, verbose_name="考试分数(满分100)")
pass_score = models.FloatField(default=0, verbose_name="通过分数(满分100)")
add_time = models.DateTimeField(auto_now=True, verbose_name="添加时间")
class ScoreCreateView(CreateView):
model = Score
form_class = ScoreForm
template_name = ''
def form_valid(self, form):
testscore = form.save(commit=False)
testscore.save()
return redirect('', score.pk)
class ScoreForm(forms.ModelForm):
user = forms.ModelChoiceField(queryset=User.objects.order_by('department', 'username'))
class Meta:
model = Score
fields = ('test', 'user', 'score', 'pass_score')