librelist archives

« back to archive

Peewee - Error when looping - ValueError invalid literal for int() with base 10: '021-0909545'

Peewee - Error when looping - ValueError invalid literal for int() with base 10: '021-0909545'

From:
azwar akbar
Date:
2012-06-17 @ 02:49
Hello.

I have problem when I make looping data model from database.


It said :
ValueError

ValueError: invalid literal for int() with base 10: '021-0909545'
....
..


*by the way: '021-0909545' is customer phone number on Customer model.


This is my code :
---------------------

@sales_order_page.route('/')
def list():
    s_order = SalesOrder().select()

    if s_order.count() is 0:
        return redirect(url_for(add))

    return render_template('list.html', s_order=s_order)


and this is my template:
-------------------------

            {% for item in s_order %}
              <tr>
                <td>{{ item.customer <http://item.customer.name> }}</td>
                <td>{{ item.order_date }}</td>
                <td>{{ item.completion }}</td>
                <td>{{ item.person }}</td>
                <td>{{ item.email }}</td>
                <td>{{ item.subject }}</td>
              </tr>
            {% endfor %}


my model:
-------------------------

class Customer(db.Model):
    """
    Table Customer
    """
    name = CharField(max_length=50, null=False)
    address = CharField(default='')
    phone = CharField(default='')
    fax = CharField(default='')

class SalesOrder(db.Model):
    """
    Table Sales Order
    """
    customer = ForeignKeyField(Customer)
    order_date = DateField()
    completion = DateField()
    person = CharField(max_length=50, default='')
    email = CharField(max_length=50, default='')
    subject = TextField(default='')
    file_name = CharField(default='', help_text="Nama file hasil scan
dokumen PO")

Can anyone give me guidance?

Thanks.


-- 


regards,
Azwar Akbar

Re: [flask] Peewee - Error when looping - ValueError invalid literal for int() with base 10: '021-0909545'

From:
Peter Ward
Date:
2012-06-17 @ 07:57
On 17 June 2012 12:49, azwar akbar <azwar.akbar@gmail.com> wrote:

> Hello.
>
> I have problem when I make looping data model from database.
>
>
> It said :
> ValueError
>
> ValueError: invalid literal for int() with base 10: '021-0909545'
> ....
> ..
>
>
> *by the way: '021-0909545' is customer phone number on Customer model.
>
>
> This is my code :
> ---------------------
>
> @sales_order_page.route('/')
> def list():
>     s_order = SalesOrder().select()
>
>     if s_order.count() is 0:


this condition is canonically written using ==, not is.
if s_order.count() == 0

(this isn’t the cause of your immediate problem, though)


>         return redirect(url_for(add))
>
>     return render_template('list.html', s_order=s_order)
>
>
> and this is my template:
> -------------------------
>
>             {% for item in s_order %}
>               <tr>
>                 <td>{{ item.customer <http://item.customer.name> }}</td>
>

Assuming nothing else is in this template, the above line is the one which
appears most suspicious:
this will be trying to print/render a Customer object: what’s in the
__str__ method (or is it __repr__, I can’t quite recall the rules here)?
Chances are, you’ve written a custom __str__ method, which tries to coerce
the Customer.photo attribute into an int.
But, I’m just guessing here: does the traceback give any more information
about where it went wrong?


>                 <td>{{ item.order_date }}</td>
>                 <td>{{ item.completion }}</td>
>                 <td>{{ item.person }}</td>
>                 <td>{{ item.email }}</td>
>                 <td>{{ item.subject }}</td>
>               </tr>
>             {% endfor %}
>
>
> my model:
> -------------------------
>
> class Customer(db.Model):
>     """
>     Table Customer
>     """
>     name = CharField(max_length=50, null=False)
>     address = CharField(default='')
>     phone = CharField(default='')
>     fax = CharField(default='')
>
> class SalesOrder(db.Model):
>     """
>     Table Sales Order
>     """
>     customer = ForeignKeyField(Customer)
>     order_date = DateField()
>     completion = DateField()
>     person = CharField(max_length=50, default='')
>     email = CharField(max_length=50, default='')
>     subject = TextField(default='')
>     file_name = CharField(default='', help_text="Nama file hasil scan
> dokumen PO")
>
> Can anyone give me guidance?
>
> Thanks.
>
>
> --
>
>
> regards,
> Azwar Akbar
>
>
>
>


-- 
Peter Ward
http://flowblok.id.au/
BIT IV, Sydney University

Re: [flask] Peewee - Error when looping - ValueError invalid literal for int() with base 10: '021-0909545'

From:
azwar akbar
Date:
2012-06-17 @ 13:14
Hi Peter, thanks for your guidance.

This is my code :
>> ---------------------
>>
>> @sales_order_page.route('/')
>> def list():
>>     s_order = SalesOrder().select()
>>
>>     if s_order.count() is 0:
>
>
> this condition is canonically written using ==, not is.
> if s_order.count() == 0
>
> thank you for explaining me to use *== *rather than *is* when comparing
number


> (this isn’t the cause of your immediate problem, though)
>
> yes it's not the cause of my problem

>
>>         return redirect(url_for(add))
>>
>>     return render_template('list.html', s_order=s_order)
>>
>>
>> and this is my template:
>> -------------------------
>>
>>             {% for item in s_order %}
>>               <tr>
>>                 <td>{{ item.customer <http://item.customer.name> }}</td>
>>
>
>  Assuming nothing else is in this template, the above line is the one
> which appears most suspicious:
> this will be trying to print/render a Customer object: what’s in the
> __str__ method (or is it __repr__, I can’t quite recall the rules here)?
> Chances are, you’ve written a custom __str__ method, which tries to coerce
> the Customer.photo attribute into an int.
> But, I’m just guessing here: does the traceback give any more information
> about where it went wrong?ut
>
>

<td>{{ item.customer <http://item.customer.name> }}</td>
I have tried to delete this line on template, but the error is still same,
should I make join?
I also try using related_name on model,
    customer = ForeignKeyField(Customer, related_name='sales_orders')
but still not working



>                 <td>{{ item.order_date }}</td>
>>                 <td>{{ item.completion }}</td>
>>                 <td>{{ item.person }}</td>
>>                 <td>{{ item.email }}</td>
>>                 <td>{{ item.subject }}</td>
>>               </tr>
>>             {% endfor %}
>>
>>
>> my model:
>> -------------------------
>>
>> class Customer(db.Model):
>>     """
>>     Table Customer
>>     """
>>     name = CharField(max_length=50, null=False)
>>     address = CharField(default='')
>>     phone = CharField(default='')
>>     fax = CharField(default='')
>>
>> class SalesOrder(db.Model):
>>     """
>>     Table Sales Order
>>     """
>>     customer = ForeignKeyField(Customer)
>>     order_date = DateField()
>>     completion = DateField()
>>     person = CharField(max_length=50, default='')
>>     email = CharField(max_length=50, default='')
>>     subject = TextField(default='')
>>     file_name = CharField(default='', help_text="Nama file hasil scan
>> dokumen PO")
>>
>> Can anyone give me guidance?
>>
>> Thanks.
>>
>>
>> --
>>
>>
>> regards,
>> Azwar Akbar
>>
>>
>>
>>
>
>
> --
> Peter Ward
> http://flowblok.id.au/
> BIT IV, Sydney University
>



-- 


regards,
Azwar Akbar

Re: [flask] Peewee - Error when looping - ValueError invalid literal for int() with base 10: '021-0909545'

From:
Simon Sapin
Date:
2012-06-17 @ 17:25
Le 17/06/2012 15:14, azwar akbar a écrit :
>
> <td>{{ item.customer <http://item.customer.name>}}</td>
> I have tried to delete this line on template, but the error is still
> same, should I make join?
> I also try using related_name on model,
>      customer = ForeignKeyField(Customer, related_name='sales_orders')
> but still not working

Can you copy the full traceback so that we see where the error is?

-- 
Simon Sapin

Re: [flask] Peewee - Error when looping - ValueError invalid literal for int() with base 10: '021-0909545'

From:
azwar akbar
Date:
2012-06-18 @ 04:32
Hello.

Thank you very much for your time and guidance.

The error was fixed.
It is because may mistake when profiding select field data on input
form, I was mistype. It should be ID but I type phone number, sorry.

old code:
    form.customer.choices = [(cust.phone, cust.name) for cust in customer]

I change it to:
    form.customer.choices = [(cust.id, cust.name) for cust in customer]

add coerce on selectField:
    customer = SelectField('Customer', validators=[Required()],
description="Pilih customer", coerce=int)

After change it and adding coerce=int in selectfield in validation
model, my program work fine.

Thank you.

On 6/18/12, Simon Sapin <simon.sapin@exyr.org> wrote:
> Le 17/06/2012 15:14, azwar akbar a écrit :
>>
>> <td>{{ item.customer <http://item.customer.name>}}</td>
>> I have tried to delete this line on template, but the error is still
>> same, should I make join?
>> I also try using related_name on model,
>>      customer = ForeignKeyField(Customer, related_name='sales_orders')
>> but still not working
>
> Can you copy the full traceback so that we see where the error is?
>
> --
> Simon Sapin
>
>
>


-- 


regards,
Azwar Akbar