【发布时间】:2015-11-01 19:25:03
【问题描述】:
我在 Django 1.8 中做一个基本应用程序,我正在使用 Create-View,我不知道为什么创建表单没有 manyTOmany 字段,也没有以前在我的模型中定义的外键字段。这是我的代码:
我的模特:
class Autor(models.Model):
nombre = models.CharField(max_length=30)
....
class Editor(models.Model):
nombre = models.CharField(max_length=30)
...
class Libro(models.Model):
titulo = models.CharField(max_length=100)
autores = models.ManyToManyField(Autor) #MANY2MANY FIELD
editor = models.ForeignKey(Editor) #FOREIGN KEY FIELD
fecha_publicacion = models.DateField()
portada = models.ImageField(upload_to = 'portadas/')
def __unicode__(self):
return self.titulo
我的观点:
class LibroCreateView(CreateView):
model = Libro
template_name = 'biblioteca/crear.html'
fields = ['titulo', 'autores', 'editor', 'fecha_publicacion', 'portada']
我的模板:
{% block main %}
<form action="" enctype="multipart/form-data" method="POST">{% csrf_token %}
<table>
{{form.as_table}}
</table>
<input type="submit" name="crear" value="Crear">
</form> <br>
{% endblock main %}
为什么我的字段“Autores”(many2many)和“Editor”(外键)没有正确显示?谢谢。
【问题讨论】:
标签: django django-models django-templates django-views django-1.8