I'm trying to save on db but it doesn't work.
Here's my Models.py
from django.db import models
# Create your models here.
class Persona(models.Model):
name = models.CharField(max_length=50)
birth_date = models.DateTimeField()
reg_date = models.DateTimeField(auto_now_add=True, blank=True)
address = models.CharField(max_length=30)
phone_number = models.CharField(max_length=10)
email = models.EmailField(max_length=254)
Here's my Views.py
from django.shortcuts import render, redirect
from .forms import insert_values
from .models import Persona
from django.views.generic import TemplateView
# Create your views here.
class HomeView(TemplateView):
def post(self, request, *args, **kwargs):
try:
form = insert_values(request.POST)
if form.is_valid():
p = Persona(
name=form.cleaned_data['name'],
birth_date=form.cleaned_data['birth_date'],
address=form.cleaned_data['address'],
phone_number=form.cleaned_data['phone_number'],
email=form.cleaned_data['email'],
)
p.save()
return redirect('home')
except Exception as err:
errore = err.__str__()
The post call works and the request is redirected, but in the object p I got the exception "AttributeError: Manager isn't accessible via 'Persona' instances". How can I solve this?
This is the full exception in the debugger mode:
'Traceback (most recent call last):\n File "c:\\program files (x86)\\microsoft visualstudio\\2019\\community\\common7\\ide\\extensions\\microsoft\\python\\core\\debugpy\_vendored\\pydevd\_pydevd_bundle\\pydevd_resolver.py", line 193, in _get_py_dictionary\n attr = getattr(var, name)\n File "C:\\Users\\fboschini\\Desktop\\Django_Covid19\\v_env_covid19\\lib\\site-packages\\django\\db\\models\\manager.py", line 176, in get__\n raise AttributeError("Manager isn\'t accessible via %s instances" % cls.__name)\nAttributeError: Manager isn\'t accessible via Persona instances\n'
if I look at the terminal I have this:
[08/Sep/2020 11:36:32] "GET /favicon.ico HTTP/1.1" 404 2141
[08/Sep/2020 11:36:40] "POST / HTTP/1.1" 302 0
[08/Sep/2020 11:36:40] "GET / HTTP/1.1" 200 5846