gpt4 book ai didi

python - Django-Models:TypeError:强制转换为 Unicode:需要字符串或缓冲区,用户发现

转载 作者:太空宇宙 更新时间:2023-11-04 07:41:28 24 4
gpt4 key购买 nike

我要显示值

(last name and first name)

作为外键来自模型。我希望这些值以选择的形式出现,以便用户可以选择他们想要将对象分配给的学生的姓名。

比如我有student form和Testscore form,当老师要提交学生的考试成绩时,在test form中会有一个包含学生姓名的下拉表单,所以老师可以选择哪个学生他想把考试成绩分配给。

写完下面的代码后,我得到了这个错误:

TypeError at /submiit_test/

coercing to Unicode: need string or buffer, User found

模型.py

class Student(models.Model):
user=models.ForeignKey(User,unique=True)
pic=models.ImageField(upload_to="profile_image")
first_name=models.CharField(max_length=100)
last_name=models.CharField(max_length=100)

class TestScore(models.Model):
user=models.ForeignKey(User)
studd=models.ForeignKey(Student)
year=models.CharField(max_length=20)
subject_1=models.CharField(max_length=50, choices=SUBJECT_CHOICES)
score_1=models.CharField(max_length=50)
subject_2=models.CharField(max_length=50, choices=SUBJECT_CHOICES)
score_2=models.CharField(max_length=50)
subject_3=models.CharField(max_length=50, choices=SUBJECT_CHOICES)
score_3=models.CharField(max_length=50)

def __unicode__(self):
return unicode(self.studd)

class TestScoreForm(ModelForm):
class Meta:
model=TestScore
fields=('year','studd','subject_1','score_1','subject_2','score_2','subject_3','score_3')

View .py

def test_submit(request):
if request.method=="POST":
form=TestScoreForm(request.POST, request.FILES)
if form.is_valid():
data=form.cleaned_data
newtest=TestScore(
user=request.user,
studd=data['studd'],
year=data['year'],
subject_1=data['subject_1'],
score_1=data['score_1'],
subject_2=data['subject_2'],
score_2=data['score_2'],
subject_3=data['subject_3'],
score_3=data['score_3'])
newtest.save()
return HttpResponse('thanks')
else:
return HttpResponse('fill the form correctly')
else:
return render_to_response('posttest.html',{'TestScoreForm':TestScoreForm,'TestScore':TestScore},context_instance=RequestContext(request))

最佳答案

我已经修好了。这是从

Student

模型。我不得不将其更改为

def __unicode__(self):
return '%s' % (self.user)

关于python - Django-Models:TypeError:强制转换为 Unicode:需要字符串或缓冲区,用户发现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19600882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com