INGl0R1AM0R1
INGl0R1AM0R1

Reputation: 1628

Modelform not saving data even tho it is being executed

The other posts that have this same question didnt actually help me so i decided to ask, problem is a little weird. Bcs the form.save command is being executed, at least i think but when i take a look at my db by the admin page, it doesnt work, and i dont know y, interesting enough the data is displayed in the print and the sucess message is displayed, if any extra information is needed i will gladly give it.

Here it is the base model

class DadoDB(models.Model):
    marca=models.CharField(max_length = 30, default ="ACURA")
    modelo=models.CharField(max_length = 30, default="XWZ")
    ano=models.IntegerField(default= 2021)
    status=models.CharField(max_length= 10,default= "BOM")
    cor=models.CharField(max_length= 10, default= "VERMELHO")
    combustivel=models.CharField(max_length= 10,default= "FLEX")
    quilometragem=models.DecimalField(max_digits= 10,decimal_places=2,max_length= 12,default=100)
    lucro=models.DecimalField(max_digits= 10,decimal_places=2,max_length= 12,default=100)
    preco=models.DecimalField(max_digits= 10,decimal_places=2,max_length= 12,default=100)
    margem_de_lucro=models.DecimalField(max_digits= 10,decimal_places=2,max_length= 12,default=100)
    data_postada = models.DateTimeField(default=timezone.now)
    autor= models.ForeignKey(User, on_delete=models.CASCADE)


    def __str__(self):
        return f'{self.user.username} Empresa'

Here it is the base form

from django import forms
from .models import DadoDB

class InsiraDadosForm(forms.ModelForm):
      class Meta:
      model = DadoDB
      ['marca','modelo','ano','status','cor','combustivel'....

Here is the view

@login_required
def InserirDado(request):
    if request.method == 'POST':
       form = InsiraDadosForm(request.POST,instance= request.user)
       if form.is_valid():
             form.save()
             print(request.POST)
             messages.success(request, f'Seus dados foram inseridos com sucesso!')
             return redirect('dado-InserirDado')
    else:
       form = InsiraDadosForm()
    return render(request, 'dado/inserirdado.html', {'form': form})

Upvotes: 1

Views: 30

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476659

Using instance=request.user makes no sense. This means you are going to edit the logged in user record. But that is not a DadoDB object, so you will set attributes on the logged in user, and when saving the user, there will be no changes, not even to that suer object, since the user has no fields like cor, ano, etc.

@login_required
def InserirDado(request):
    if request.method == 'POST':
        # no instance=request.user ↓
        form = InsiraDadosForm(request.POST)
        if form.is_valid():
            form.instance.autor = request.user
            form.save()
            print(request.POST)
            messages.success(request, f'Seus dados foram inseridos com sucesso!')
            return redirect('dado-InserirDado')
    else:
       form = InsiraDadosForm()
    return render(request, 'dado/inserirdado.html', {'form': form})

Upvotes: 1

Related Questions