__str__返回的非字符串(元组类型) [英] __str__ returned non-string (type tuple)
问题描述
我有一个表格,一直在django中向我抛出错误,我尝试过在模型上在线搜索尝试过的str(),但根本无法工作。谷歌搜索几次尝试了几种不同的方法,但是每次我单击表单的链接时,都无法正常工作,但仍然无法得到相同的Django错误页面。
I have a form that keeps throwing me an error in django, Ive tried searching online tried str() on my models but wouldnt work at all. Googled a couple of times tried a couple different methods but none worked still get the same django error page everytime i click the link to the form.
TypeError: __str__ returned non-string (type tuple)
我的模型
# Injury Parameters -> SOI Level 2 #
####################################
class SourceOfInjuryLevel2(models.Model):
creator = models.ForeignKey('auth.User')
id = models.AutoField(primary_key=True)
soi_l1 = models.CharField(max_length=80)
soi_l2 = models.CharField(max_length=80)
status = models.CharField(max_length=8)
created_date = models.DateTimeField(default=timezone.now)
modified_date = models.DateTimeField(blank=True, null=True)
modified_by = models.CharField(max_length=60, blank=True, null=True)
def create(self):
self.save()
def __str__(self):
return self.soi_l1, self.soi_l2, self.status
我的表格
# Soure of Injury Level 2 #
###########################
class SourceOfInjuryLevel2Form(forms.ModelForm):
options = (('Enabled', 'Enabled',), ('Disabled', 'Disabled'))
soi_l1 = forms.ModelChoiceField(
queryset=SourceOfInjuryLevel1.objects.filter(status='Enabled'),
widget=forms.Select(attrs={'class': 'form-control'})
)
soi_l2 = forms.CharField(
widget=forms.TextInput(attrs={'class': 'form-control'})
)
status = forms.CharField(
widget=forms.Select(
attrs={'class': 'form-control'},
choices=options
)
)
class Meta:
model = SourceOfInjuryLevel2
fields = ('soi_l1', 'soi_l2', 'status')
我的视图
# New Source of Injury Level 2 #
################################
def new_source_of_injury_level2(request):
form = SourceOfInjuryLevel2Form()
if request.method == "POST":
form = SourceOfInjuryLevel2Form(request.POST)
if form.is_valid():
source_of_injury_level2 = form.save(commit=False)
source_of_injury_level2.creator = request.user
source_of_injury_level2.created_date = timezone.now()
source_of_injury_level2.save()
messages.success(request, 'Object Has Been Created')
return redirect(injury_parameters)
else:
messages.error(request, 'Object Has Not Been Created')
else:
form = SourceOfInjuryLevel2Form()
return render(request,
'process_injury_management/source_of_injury_level2.html',
{'form': form,
'title': 'New Source of Injury Level 2'})
推荐答案
模型中有错误:
class SourceOfInjuryLevel2(models.Model):
...
def __str__(self):
return self.soi_l1, self.soi_l2, self.status
我想您很困惑,因为Python 2打印语句看起来像将元组转换为字符串,但这实际上不是print语句的工作方式-这是一个令人困惑的细节,在Python 3中已更改。
I guess you were confused because the Python 2 print statement looks like it turns tuples into strings, but that's not actually how the print statement works - it's a confusing detail that was changed in Python 3.
请尝试以下操作:
def __str__(self):
template = '{0.soi_l1} {0.soi_l2} {0.status}'
return template.format(self)
这篇关于__str__返回的非字符串(元组类型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!