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

I have created form, in which users can add some kind of information. But, I noticed that one user can add only one object. I mean, after secod time of filling out a form, new infermation displayed, but old record was deleted.

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})

SO as I said, when one user fills out form more than one time, in the list page there are only last added record. I guess that problem in views.py at customer = File.objects.get(customer=request.user). I know that get() give only one object, so I had changed get() to filter(). But I have a error: 'QuerySet' object has no attribute '_meta' and it specify on **formset = FileForm(request.POST, instance=customer) ** line. I hope that will help you). Thank you in advance.

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 %}

Upvotes: 1

Views: 382

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477685

You are editing a record by constructing your FileForm(…, instance=…). If you want to create a new record, you should not pass an instance.

The view thus should look like:

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

    form = FileForm()

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

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

Upvotes: 0

Related Questions