我需要保存以下模型:
def validate_pdf(value):
if not value.name.endswith('.pdf'):
raise ValidationError("O arquivo deve ser no formato PDF.")
class Documento(models.Model):
titulo = models.CharField(max_length=200)
autor = models.CharField(max_length=200)
instituicao = models.CharField(max_length=200)
arquivo_pdf = models.FileField(validators=[validate_pdf])
tamanho = models.BigIntegerField(default=1)
and that's my creation创建view视图:
class DocumentoCreateView(CreateView):
model = Documento
fields = (
"titulo",
"autor",
"instituicao",
"arquivo_pdf"
)
template_name = "documentos/documento_new.html"
success_url = reverse_lazy("documento_new")
def post(self, request, *args, **kwargs):
super(DocumentoCreateView, self).post(request)
form = DocumentoCreateForm(request.POST, request.FILES)
print(form.errors)
if form.is_valid():
documento = form.save()
#documento.save()
path = f'/media/' + form.files['arquivo_pdf'].__str__()
extracao_de_texo = TextExtractor(path)
这是我用来创建模型的表单:
class DocumentoCreateForm(forms.ModelForm):
class Meta:
model = Documento
fields = [
"titulo",
"autor",
"instituicao",
"arquivo_pdf"
]
我的问题是,即使form.is_valid()为false,模型也会保存,因此这意味着它在验证之前保存。
我期望模型只在我调用form.save()方法时才被插入。我不知道这是什么问题。
1条答案
按热度按时间oxiaedzo1#
你得把线拿掉
super(DocumentoCreateView, self).post(request)