1
我正在django中開發一個應用程序。這是我的models.py和views.py代碼:Django無法隱式將'Recipe_instruction'對象轉換爲str
#models.py
class Recipe_instruction(models.Model):
content = models.TextField(max_length=500)
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE)
order = models.IntegerField(max_length=500)
class Meta:
app_label='recipe_base'
def __str__(self):
return self.content
views.py
#create recipes_dict
...
recipe_instructions = Recipe_instruction.objects.filter(recipe = recipe)
recipe_instructions_string = ""
for recipe_instruction in recipe_instructions:
recipe_instructions_string = recipe_instructions_string + recipe_instruction.content
...
我的目標是讓所有的配方說明,並一起固定這些成一個字符串recipe_instructions_string
但是,當我跑我的views.py,它給了我下面的錯誤:
recipe_instructions_string = recipe_instructions_string + recipe_instruction.content
TypeError: Can't convert 'Recipe_instruction' object to str implicitly
可以在你告訴我發生了什麼事?
由於recipe_instruction.content是一個文本字段,所以我不需要再次將它轉換爲一個字符串,因爲它已經是一個字符串。
TRACEBACK:
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/celery/app/trace.py", line 240, in trace_task
R = retval = fun(*args, **kwargs)
File "/usr/local/lib/python3.4/dist-packages/celery/app/trace.py", line 438, in __protected_call__
return self.run(*args, **kwargs)
File "/root/worker/worker/views.py", line 500, in Task1
recipe_instructions_string = recipe_instructions_string + recipe_instruction.content
TypeError: Can't convert 'Recipe_instruction' object to str implicitly
你能粘貼確切的堆棧跟蹤嗎?看起來你所顯示的代碼是錯誤發生的地方? – karthikr
TypeError(「無法將'Recipe_instruction'對象隱式轉換爲str」)是我得到的異常。是的,我做了更改後重新啓動它 – Elisha512
Recipe_instruction>食譜指導 – allcaps