Fwd: Delivery Status Notification (Failure)

31 views
Skip to first unread message

Nishant Sagar

unread,
Sep 17, 2022, 3:54:49 PM9/17/22
to django...@googlegroups.com

Hey folks,
While saving the values to the data to the database I'm getting this Field 'roof_age' expected a number but got ('1',)

How can I resolve this?
Here is my views.py file
def form(requests):
if requests.method == 'POST':
name=requests.POST['name'],
# roof_age= requests.POST['roof_age'],
roof_age= int(requests.POST.get('roof_age')),
email = requests.POST['email'],
phone = requests.POST['phone'],
address = requests.POST['address'],
monthly_bill = requests.POST['monthly_bill'],
HOA = requests.POST['HOA'],
battery = requests.POST['battery'],
foundation = requests.POST['foundation'],
roof_type = requests.POST['roof_type'],
availability= requests.POST['availability'],
bill = requests.POST['bill']

details = Details(
name = name,
roof_age = roof_age,
email = email,
phone = phone,
address = address,
monthly_bill = monthly_bill,
HOA = HOA,
battery = battery,
foundation = foundation,
roof_type = roof_type,
availability= availability,
bill = bill
)
print(type(roof_age))
print(name, roof_age, email, phone, address, monthly_bill, HOA,battery, foundation, roof_type, availability, bill)
print("The data has been save to db")

The type of roof_age I'm getting is tuple

Here is my models.py file 

class Details(models.Model):
name = models.CharField(max_length=25)
roof_age = models.IntegerField()
email = models.EmailField()
phone = models.IntegerField()
address = models.TextField(max_length=100)
monthly_bill = models.IntegerField()
HOA = models.BooleanField(default=True)
battery = models.BooleanField(default=True)
foundation = models.BooleanField(default=True)
roof_type = models.CharField(max_length=20)
availability = models.DateTimeField()
bill = models.FileField(upload_to='uploads/%d/%m/%y/')

class Meta:
verbose_name = 'details'
verbose_name_plural = 'details'

def __str__(self):
return self.name

Thank You

Ross Meredith

unread,
Sep 17, 2022, 4:37:50 PM9/17/22
to django...@googlegroups.com
This is the wrong approach.  You should use a model form. 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CANNtL-JChT587N_h7stdkiUYHNHTDhQGgPLzZW%3D-QuAcYgiocw%40mail.gmail.com.

Ryan Nowakowski

unread,
Sep 17, 2022, 6:25:36 PM9/17/22
to django...@googlegroups.com

On Sat, Sep 17, 2022 at 11:48:06PM +0530, Nishant Sagar wrote:
> While saving the values to the data to the database I'm getting this Field
> 'roof_age' expected a number but got ('1',)
>
> How can I resolve this?
> Here is my views.py file
> def form(requests):
> if requests.method == 'POST':
> name=requests.POST['name'],
> # roof_age= requests.POST['roof_age'],
> roof_age= int(requests.POST.get('roof_age')),

You have an errant comma at the end of the line which makes roof_age a
tuple. Get rid of the comma.

Also, I agree with the other reply that you should probably be using a
Form[1] to handle POST input.

[1] https://docs.djangoproject.com/en/4.1/topics/forms/

Derek

unread,
Sep 19, 2022, 1:30:33 AM9/19/22
to Django users
I agree with previous posts.

It might also be worthwhile to simplify your code to avoid unneeded variables:

        details=Details(
            name=requests.POST.get('name'),
            roof_age=requests.POST.get('roof_age'),
            email=requests.POST.get('email'),
            phone=requests.POST.get('phone'),
            address=requests.POST.get('address'),
            monthly_bill=requests.POST.get('monthly_bill'),
            HOA=requests.POST.get('HOA'),
            battery=requests.POST.get('battery'),
            foundation=requests.POST.get('foundation'),
            roof_type=requests.POST.get('roof_type'),
            availability=requests.POST.get('availability'),
            bill=requests.POST.get('bill')
        )
        print(details)
        details.clean()
        details.save()
        print("The data has been saved to db")

Reply all
Reply to author
Forward
0 new messages