为什么一个用户只能在django的表单中添加一条记录?

Why does one user can add only one record in form in django?

我创建了表单,用户可以在其中添加某种信息。但是,我注意到一个用户只能添加一个对象。我的意思是,在第二次填写表格后,显示了新的信息,但旧的记录被删除了。

models.py:

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

class File(models.Model):
customer = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
graf_title = models.CharField('Название файла', max_length = 200)
chart = models.TextField('Значения осей')
title = models.CharField('Название Графика', max_length = 50)
x_title = models.CharField('Название Оси Х', max_length = 50)
y_title = models.CharField('Название Оси Y', max_length = 50)

forms.py:

from .models import File
from django import forms

class FileForm(ModelForm):
    class Meta:
         model = File
         fields = '__all__'
         exclude = ['customer']

views.py:

#It is the form view
def main(request):

    error = ''
    customer = File.objects.get(customer=request.user)
    formset = FileForm(request.POST)

    if request.method == 'POST':
        formset = FileForm(request.POST, instance=customer)
        if formset.is_valid():
            formset.save()

            return redirect('grafic:list')
        else:
            error = 'Форма была неверной('

    formset = FileForm()

    data = {
        'form': formset,
        'error': error,
    }

    return render(request, 'graf/first.html', data)

#list is the view, which show all user's records
def list(request):

    record= File.objects.filter(customer=request.user)

    return render(request, 'graf/list.html', {'record': record})

所以正如我所说,当一个用户多次填写表单时,列表页面中只有最后添加的记录。我猜 views.py 中的问题在 customer = File.objects.get(customer=request.user)。我知道 get() 只给出一个对象,所以我将 get() 更改为 filter()。但是我有一个错误:'QuerySet' object has no attribute '_meta' 并在 **formset = FileForm(request.POST, instance=customer) ** 行上指定。我希望这会帮助你)。提前谢谢你。

list.html:

{% for records in record %}

<article class="card">
    <div class="card_wrapper">                  
        <figure class="card_feature">
            <a href="{% url 'grafic:index' records.id %}"></a>
        </figure>

        <div class="card_box">
            <a href="{% url 'grafic:index' records.id %}">
            <header class="card_item card_header">
                <h6 class="card__item card__item--small card__label">Твой график</h6>
                <a class="card__item card__item--small card__title" href="{% url 'grafic:index' records.id %}">
                    {{records.graf_title}}
                </a>
            </header>
                <hr class="card__item card__divider">   
            </a>                        
        </div>
    </div>
</article>      
    
{% endfor %}

您正在通过构建 FileForm(…, instance=…) 来编辑记录。如果要创建 new 记录,则不应传递实例。

视图应如下所示:

def main(request):
    error = ''
    if request.method == 'POST':
        # no <i>instance=customer</i>
        form = FileForm(request.POST, request.FILES)
        if form.is_valid():
            form<b>.instance.customer = request.user</b>
            form.save()
            return redirect('grafic:list')
        else:
            error = 'Форма была неверной('

    form = FileForm()

    data = {
        'form': form,
        'error': error,
    }

    return render(request, 'graf/first.html', data)