django 如何删除以前上传的文件时,一个新的得到上传使用id

q7solyqu  于 2023-05-08  发布在  Go
关注(0)|答案(2)|浏览(91)

我有一个简单的简历上传类,让用户上传他们的简历。它工作得很好,但当他们上传一个新的,以前的不会被删除。这是我的代码:

class ResumeDocument(models.Model):
    id = models.AutoField(primary_key=True)
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    cvfile = models.FileField(upload_to="documents", null=True, validators= [validate_file_extension])

    @property
    def filename(self):
        return os.path.basename(self.cvfile.name)

我如何才能找到以前的ID?id = self.id- 1.类似的东西。
这是我的观点:

@login_required
def pdf_resume(request):
    if request.method == 'POST':
        form = DocumentForm(request.POST,request.FILES)
        if form.is_valid():
            form.instance.user = request.user
            form.save()
            return redirect('pdf_resume')
        if 'delete' in request.GET:
            return delete_item(ResumeDocument, request.GET['id'])

    else:
        form = DocumentForm()
    documents = ResumeDocument.objects.filter(user=request.user)
    if documents:
        form = DocumentForm(instance=documents[0])
    context = {
        'form':form,
        'documents':documents,
    }
    return render(request, 'reg/pdf_resume.html', context)

这也是我的HTML代码:

<form id="document-form" method="POST" enctype="multipart/form-data" action="{% url 'pdf_resume' %}" autocomplete="off" class="ant-form ant-form-horizontal">
                {% csrf_token %}
                <p>{{ form }}</p>
              

                <div class="ant-row-flex" style="margin-left: -10px; margin-right: -10px;"></div>
                <button id="btn_submit" type="submit"
                    class="ant-btn ant-btn-primary"
                    ant-click-animating-without-extra-node="false" style="float: left;"><span>upload</span></button>
            </form>
41zrol4v

41zrol4v1#

一个可能的解决方案是保存每个用户的所有上传的CV,并使用字段uploaded_on跟踪最后上传的CV。您可以参考下面的高级示例:

class CVFile(models.Model):
    file_name = models.CharField(max_length=200)
    cv_file = models.FileField(upload_to="documents", null=True, validators=[validate_file_extension])
    uploaded_on = models.DateTimeField(default=timezone.now, blank=True)

class ResumeDocument(models.Model):
    id = models.AutoField(primary_key=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    cv_file = models.ForeignKey(CVFile, on_delete=models.CASCADE)

您需要相应地修改您的HMTLviews.py

bvhaajcl

bvhaajcl2#

models.py

class ResumeDocument(models.Model):
    id = models.AutoField(primary_key=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    cvfile = models.FileField(upload_to="documents", null=True, validators= 
    [validate_file_extension])

views.py(我只是看看是否有任何resumedocuments为该用户,如果是,所以我给予他的形式,只是如果他正在编辑现有的文件)

@login_required
def pdf_resume(request):
    if request.method == 'POST':
        form = DocumentForm(request.POST,request.FILES)
        if form.is_valid():
            form.instance.user = request.user
            form.save()
            return redirect('pdf_resume')
        if 'delete' in request.GET:
            return delete_item(ResumeDocument, request.GET['id'])

    else:
        form = DocumentForm()
    documents = ResumeDocument.objects.filter(user=request.user)
    if documents:#new
        form = DocumentForm(instance=documents[0])
    context = {
        'form':form,
        'documents':documents,
    }
    return render(request, 'reg/pdf_resume.html', context)

现在在你的pdf_resume. html里面你可以只传递'form',因为你已经检查了那个用户是否有文档。

相关问题