[英]Django template rendering error in Admin with ManyToMany field
Django 在管理界面中為我的模型之一渲染模板時輸出錯誤。 具體來說,我試圖在模型中添加一個新條目,但出現以下錯誤和回溯:
Template error:
In template C:\ProgramData\Anaconda3\envs\django\lib\site-packages\django\contrib\admin\templates\admin\includes\fieldset.html, error at line 19
'int' object is not iterable
9 : {% for field in line %}
10 : <div{% if not line.fields|length_is:'1' %} class="fieldBox{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% if not field.is_readonly and field.errors %} errors{% endif %}{% if field.field.is_hidden %} hidden{% endif %}"{% elif field.is_checkbox %} class="checkbox-row"{% endif %}>
11 : {% if not line.fields|length_is:'1' and not field.is_readonly %}{{ field.errors }}{% endif %}
12 : {% if field.is_checkbox %}
13 : {{ field.field }}{{ field.label_tag }}
14 : {% else %}
15 : {{ field.label_tag }}
16 : {% if field.is_readonly %}
17 : <div class="readonly">{{ field.contents }}</div>
18 : {% else %}
19 : {{ field.field }}
20 : {% endif %}
21 : {% endif %}
22 : {% if field.field.help_text %}
23 : <div class="help">{{ field.field.help_text|safe }}</div>
24 : {% endif %}
25 : </div>
26 : {% endfor %}
27 : </div>
28 : {% endfor %}
29 : </fieldset>
回溯可以在這里查看
這是我的模型(博客條目和作者):
class Entry(models.Model):
entry_title = models.CharField(max_length=100)
entry_date = models.DateTimeField('date published')
entry_abstract = models.TextField(max_length=500)
entry_thumbnail = models.ImageField(default='default.png',
upload_to='static/blog/images',
blank=True)
raw_entry = models.FileField(upload_to=raw_directory_path)
html_content = models.FileField(blank=True)
entry_slug = models.SlugField(unique=True)
entry_authors = models.ManyToManyField(Author,
default=default_author)
class Author(models.Model):
first_name = models.CharField(max_length=100)
middle_name = models.CharField(max_length=100, blank=True)
last_name = models.CharField(max_length=100)
head_shot = models.ImageField(upload_to='blog/authors', blank=True)
email = models.EmailField(blank=True)
我最初認為該錯誤是由使用 admin.ModelAdmin.list_display 等屬性修改我的管理界面引起的(請參閱此處和此處),但我禁用了添加自定義管理界面,錯誤仍然存在。
我將繼續閱讀 django 中的管理界面文檔。 在 Django 管理中允許多對多字段有什么特別的我需要做的嗎?
我發現了問題。 我在我的 ManyToMany 字段中指定了一個默認值,如下所示:
def default_author():
"""get or create the pk of the deafult author. This should
be an author with name Anonymous"""
try:
author = Author.objects.get(first_name__exact='Anonymous',
last_name__exact='Author')
except ObjectDoesNotExist:
author = Author(first_name='Anonymous',
last_name='Author')
author.save()
return author.id
class Entry(models.Model):
entry_title = models.CharField(max_length=100)
entry_date = models.DateTimeField('date published')
entry_abstract = models.TextField(max_length=500)
entry_thumbnail = models.ImageField(default='default.png',
upload_to='static/jv_blog/images',
blank=True)
raw_entry = models.FileField(upload_to=raw_directory_path)
html_content = models.FileField(blank=True)
entry_slug = models.SlugField(unique=True)
entry_authors = models.ManyToManyField(Author,
default=default_author)
在 ManyToMany 字段中設置默認值的正確方法是重新實現模型 save() 方法。 如果我記得這樣做,我會用我的解決方案更新這個答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.