显示将模型实例显示为行的表格,并让用户选择要删除的实例(行)

问题描述 投票:1回答:1

在我的应用中,用户可以上传文件。这些文件由具有相关元数据属性的模型表示(上载时间,文件名,用户创建的注释,最大值等)。

用户将上传几个文件,我想向他们显示他们上传的文件的表格,每行旁边都有一个复选框,可用于删除所选文件和关联的模型实例。

我不确定正确的方法是什么,我研究了以下选项,但似乎没有明显的解决方案:

  1. 模型表格,使用CheckboxSelectMultiple

  2. django_tables2-似乎是已建立的第三方应用程序

  3. 重新使用Django管理代码(窗体和视图)。

默认django admin应用程序的行为非常适合我的用例,但是我不确定最好的再现方法是什么?

app / models.py

import uuid

from django.contrib.auth import get_user_model
from django.db import models

User = get_user_model()


class Document(models.Model):
    def rename_file(self, filename):
        ext = filename.split('.')[-1]
        new_name = uuid.uuid4().hex

        return f'documents/{new_name}.{ext}'

    owner = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        editable=True,
    )
    document = models.FileField(upload_to=rename_file)
    notes = models.CharField(max_length=258, blank=True)
    uploaded_at = models.DateTimeField(auto_now_add=True)
    nice_name = models.CharField(max_length=128, null=True, blank=False)
    start_date = models.DateField(null=True, blank=False)
    end_date = models.DateField(null=True, blank=False)

    def __str__(self):
        return str(self.document)

app / admin.py

from django.contrib import admin

from .models import Document


def uuid(obj):
    return obj.owner.uuid


uuid.short_description = 'user id'


@admin.register(Document)
class DocumentAdmin(admin.ModelAdmin):
    fields = ('document', 'notes')
    list_display = (
        'owner',
        uuid,
        'document',
        'nice_name',
        'notes',
        'uploaded_at',
        'start_date',
        'end_date'
    )

app / forms.py

from django import forms
from django.forms.widgets import CheckboxSelectMultiple

from .models import Document


class DeleteDocumentsForm(forms.ModelForm):

    document = forms.ModelMultipleChoiceField(queryset=None, widget=forms.CheckboxSelectMultiple)
    nice_name = forms.ModelMultipleChoiceField(queryset=None, widget=forms.CheckboxSelectMultiple)


    def __init__(self, *args, **kwargs):
        user = kwargs.pop('user', None)
        qs = Document.objects.filter(owner_id=user)
        #  super(DeleteDocumentsForm, self).__init__(*args, **kwargs)
        super().__init__(*args, **kwargs)
        self.fields['document'].queryset = qs
        self.fields['nice_name'].queryset = qs.values('nice_name')

    #  document.fields['nice_name'].widget.attrs['readonly'] = True

    class Meta:
        model = Document
        fields = ('document', 'nice_name')
        widgets = {
            'document': CheckboxSelectMultiple,
            'nice_name': CheckboxSelectMultiple,
        }
python django python-3.x django-forms django-tables2
1个回答
0
投票

要实现您想做的事情,您需要创建一个封装表的表单。当for循环遍历数据库中的每个项目时,请提供一个复选框,该值将是上载文件的唯一ID。通过提交表单,您还将随后提交这些ID。

所以这就是您的观点:

<form action="." method="post">
   {% csrf_token %}
   <table>
      {% for upload in uploads %}
         <tr>
            <td><input type="checkbox" name="selected" value="{{ upload.id }}"></td>
            <td><img src="{{ upload.url }}" alt="image"></td>
            <td>{{ upload.name }}</td>
          </tr>
       {% endfor %}
   </table>
   <button type="submit">Delete</button>
</form>

一旦用户选择了一个或多个复选框,然后按Delete键,您将收到一个查询字典,其中包含可以在视图中根据需要处理的所选项目的ID:

<QueryDict: {'csrfmiddlewaretoken': [''], 'selected': ['2', '3']}>

至于表格,我真的没有看到它对IMO的任何使用。

© www.soinside.com 2019 - 2024. All rights reserved.