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)
my model
# 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
my form
# 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')
My Views
# 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'})
Those commas aren't actually doing what you think they do. The commas make your return value a tuple instead of a string which a __str__
method is supposed to return.
You can instead do:
def __str__(self):
return '%s %s %s'%(self.soi_l1, self.soi_l2, self.status)
Or use the new-style formatting:
def __str__(self):
return '{} {} {}'.format(self.soi_l1, self.soi_l2, self.status)
The error is in your model:
class SourceOfInjuryLevel2(models.Model):
...
def __str__(self):
return self.soi_l1, self.soi_l2, self.status
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.
Try this instead:
def __str__(self):
template = '{0.soi_l1} {0.soi_l2} {0.status}'
return template.format(self)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With