我的模型看起来像这样:
class ComicSeries(models.Model):
"""Model definition for ComicSeries."""
# TODO: Define fields here
user = models.ForeignKey(User, on_delete=models.CASCADE,
null=True, blank=True, verbose_name='Uploaded by: '
)
title = models.CharField(verbose_name='Series Title', max_length=500)
cover = models.ImageField(verbose_name='Series cover', upload_to='comic_series',
height_field=None, width_field=None, max_length=None
)
description = models.TextField(verbose_name='Description')
artist = models.CharField(verbose_name='Artist(s)', max_length=500)
date_uploaded = models.DateTimeField(auto_now_add=True)
slug = models.SlugField(default='')
class ComicIssue(models.Model):
"""Model definition for ComicIssue."""
# TODO: Define fields here
user = models.ForeignKey(User, on_delete=models.CASCADE,
null=True, blank=True, verbose_name='Uploaded by: '
)
title = models.ForeignKey(ComicSeries, on_delete=models.CASCADE, verbose_name='Series Title')
issue = models.CharField(verbose_name='Issue Number', max_length=500)
issue_title = models.CharField(verbose_name='Issue Title', max_length=1000)
issue_cover = models.ImageField(verbose_name='Issue cover', upload_to='comic_issues', height_field=None, width_field=None, max_length=None)
issue_description = models.TextField(verbose_name='Description')
issue_file = models.FileField(verbose_name='Issue file', upload_to='comic_issues_files', max_length=100,
help_text='File in pdf or as single image'
)
is_favorite = models.BooleanField(default=False)
issue_slug = models.SlugField(default='')
views.py:
class ComicIssueCreate(LoginRequiredMixin, CreateView):
model = ComicIssue
fields = ['issue_title', 'issue_cover', 'issue_description', 'issue_cover', 'issue_file']
def form_valid(self, form):
form.instance.user = self.request.user
return super(ComicIssueCreate, self).form_valid(form)
我可以在Django admin中选择一个ComicIssue属于哪个ComicSeries。
In django admin there is an option to upload
But on my form, there is no field when I add 'title'
模板:
{% block body %}
<div class="container">
<h2>Add new comic issue/chapter</h2>
<form class="form", action="", method="POST", enctype="multipart/form-data">
{% csrf_token %}
{% for field in form %}
<div class="form-group form">
<span class="text-danger small">
{{field.errors}}
</span>
</div>
<label class="control-label col-sm-2">
{{field.label_tag}}
{{field.help_text}}
</label>
<div class="col-sm-10">{{field}}</div>
{% endfor %}
<button type="submit" class="btn grey-text black">Add</button>
</form>
</div>
{% endblock body %}
但我在自定义表单中执行此操作时遇到问题。有没有办法可以使用CreateView确定问题属于哪个系列在自定义表单中?
答案 0 :(得分:0)
ComicIssueCreate中的字段应包含标题
所以字段= [&#39;标题&#39;,&#39; issue_title&#39;,&#39; issue_cover&#39;,&#39; issue_description&#39;,&#39; issue_cover&#39 ;,&#39; issue_file&#39;]
答案 1 :(得分:0)
您在视图中指定的字段中错过了“标题”。由于 title 是ComicIssue对ComicSeries的外键,您需要在字段中包含它以实现所需的内容
class ComicIssueCreate(LoginRequiredMixin, CreateView):
model = ComicIssue
fields = ['title', 'issue_title', 'issue_cover', 'issue_description',
'issue_cover', 'issue_file']
def form_valid(self, form):
form.instance.user = self.request.user
return super(ComicIssueCreate, self).form_valid(form)
更新: 问题是由于没有在MaterialiseCSS中初始化'select'。有必要选择在MaterialiseCSS
中工作的字段 <script>
$(document).ready(function() {
$('select').material_select();
});
</script>