我整天都在努力。我正在尝试提出一种解决方案,以一种形式保存多张图片。我感觉自己很忙,但是现在的问题是,我一生都无法在提交表单时让ForeignKey自动分配。除了未将FK应用于数据库记录之外,以下代码在所有方面均起作用。任何帮助将不胜感激!
尝试通过:
FA_Attachments.objects.create(
image=f,field_evaluations = request.POST.get('order'))
只要给我:
Cannot assign "'test'": "FA_Attachments.field_evaluation" must be a "FieldEvaluation" instance.
Models.py
from django.db import models
class FieldEvaluation(models.Model):
"Our default field evaluation form"
created_at = models.DateTimeField(auto_now_add=True)
order = models.CharField(max_length=50)
class FA_Attachments(models.Model):
field_evaluation = models.ForeignKey(
FieldEvaluation, on_delete=models.CASCADE, null=True)
image = models.ImageField(upload_to='photos')
Forms.py
from django import forms
from .models import FieldEvaluation, FA_Attachments
class FieldEvaluationForm(forms.ModelForm):
class Meta:
model = FieldEvaluation
fields = ['order'] # not attachments!
attachments = forms.ImageField(
widget=forms.ClearableFileInput(attrs={'multiple': True}))
Views.py
from django.views.generic.edit import CreateView
from django.shortcuts import render
from .forms import *
from .models import *
class FieldEvaluationView(CreateView):
model = FieldEvaluation
form_class = FieldEvaluationForm
template_name = 'QA.html'
success_url = '?success'
def post(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
files = request.FILES.getlist('attachments')
if form.is_valid():
for f in files:
FA_Attachments.objects.create(
image=f)
return self.form_valid(form)
else:
return self.form_invalid(form)
答案 0 :(得分:1)
您不保存ForeignKey!
首先,我建议您将附件关系设为必填项:
class FA_Attachments(models.Model):
field_evaluation = models.ForeignKey(
FieldEvaluation, on_delete=models.CASCADE)
image = models.ImageField(upload_to='photos')
我删除了null=True
,因为此模型与FieldEvaluation紧密相关
然后,您必须修改POST。
class FieldEvaluationView(CreateView):
model = FieldEvaluation
form_class = FieldEvaluationForm
template_name = 'QA.html'
success_url = '?success'
def post(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
files = request.FILES.getlist('attachments')
# your form must have it!
order = request.POST.get('order')
fe = FieldEvaluation.objects.create(order=order)
if form.is_valid():
for f in files:
# here you create the relation with the FieldEvaluation
FA_Attachments.objects.create(
image=f, field_evaluation=fe)
return self.form_valid(form)
else:
return self.form_invalid(form)
答案 1 :(得分:1)
我会使用覆盖form_valid
而不是post
方法。在form_valid
中,您可以保存field_evaluation对象,然后可以将此新对象分配给ForeignKey字段。最后,您调用并返回form_valid
(以form作为参数)。
class FieldEvaluationView(CreateView):
model = FieldEvaluation
form_class = FieldEvaluationForm
template_name = 'QA.html'
success_url = '?success'
def form_valid(self, form):
field_evaluation_obj = form.save(commit=True)
files = self.request.FILES.getlist('attachments')
for f in files:
FA_Attachments.objects.create(image=f, field_evaluation=field_evaluation_obj)
return super(FieldEvaluationView, self).form_valid(form)