[help]django's FloatField can't insert the mysql's double type data.

1,095 views
Skip to first unread message

Korobase

unread,
May 14, 2011, 11:18:17 AM5/14/11
to django...@googlegroups.com
Hi,all!
I use the django admin interface to insert a record to the mysql database,
While the field is FloatField in django and the mysql field show a double type,
But when I click the save button,it tell me that:

Environment:


Request Method: POST

Django Version: 1.3
Python Version: 2.6.6
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'tuang.main',
 'django.contrib.admin',
 'django.contrib.admindocs']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'learndjango.middleware.GetUserLoactionMiddleware')


Traceback:
File "C:\Python26\lib\site-packages\django\core\handlers\base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "C:\Python26\lib\site-packages\django\contrib\admin\options.py" in wrapper
  307.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Python26\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  93.                     response = view_func(request, *args, **kwargs)
File "C:\Python26\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
  79.         response = view_func(request, *args, **kwargs)
File "C:\Python26\lib\site-packages\django\contrib\admin\sites.py" in inner
  197.             return view(request, *args, **kwargs)
File "C:\Python26\lib\site-packages\django\utils\decorators.py" in _wrapper
  28.             return bound_func(*args, **kwargs)
File "C:\Python26\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  93.                     response = view_func(request, *args, **kwargs)
File "C:\Python26\lib\site-packages\django\utils\decorators.py" in bound_func
  24.                 return func(self, *args2, **kwargs2)
File "C:\Python26\lib\site-packages\django\db\transaction.py" in inner
  217.                 res = func(*args, **kwargs)
File "C:\Python26\lib\site-packages\django\contrib\admin\options.py" in add_view
  882.                 self.save_model(request, new_object, form, change=False)
File "C:\Python26\lib\site-packages\django\contrib\admin\options.py" in save_model
  665.         obj.save()

Exception Type: TypeError at /admin/main/learndjango/add/
Exception Value: 'float' object is not callable

Any reply is appreciated.Thanks.


--

Andy McKay

unread,
May 14, 2011, 12:29:37 PM5/14/11
to django...@googlegroups.com

> Exception Type: TypeError at /admin/main/learndjango/add/
> Exception Value: 'float' object is not callable


Please paste your model that is having the problem.
--
Andy McKay
an...@clearwind.ca
twitter: @andymckay

Korobase

unread,
May 14, 2011, 9:10:07 PM5/14/11
to django...@googlegroups.com


2011/5/15 Andy McKay <an...@clearwind.ca>


> Exception Type: TypeError at /admin/main/learndjango/add/
> Exception Value: 'float' object is not callable


Please paste your model that is having the problem.

class MyItem(models.Model):
    title=models.CharField(max_length=1024)
    description=models.CharField(max_length=1024)
    price=models.FloatField(default=0.0)
    time=models.DateTimeField()
    name=models.CharField(max_length=256)
    detail=models.CharField(max_length=1024)
    address_city=models.CharField(max_length=36)
    address_area=models.CharField(max_length=36)

    
    def __unicode__(self):
        return self.title

and the ModelAdmin:

class MyItemAdmin(admin.ModelAdmin):
    list_display=('name','title','price','address_city',)

admin.site.register(MyItem, MyItemAdmin)

Thanks.
 
--
 Andy McKay
 an...@clearwind.ca
 twitter: @andymckay


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




-- 

Andy McKay

unread,
May 14, 2011, 11:01:47 PM5/14/11
to django...@googlegroups.com

On 2011-05-14, at 6:10 PM, Korobase wrote:
    price=models.FloatField(default=0.0)

You really should really be using a DecimalField for storing prices, not a FloatField.


Not sure off the top of my head why you get that error though.
--

Karen Tracey

unread,
May 15, 2011, 12:44:07 AM5/15/11
to django...@googlegroups.com
On Sat, May 14, 2011 at 9:10 PM, Korobase <boye...@gmail.com> wrote:


2011/5/15 Andy McKay <an...@clearwind.ca>

> Exception Type: TypeError at /admin/main/learndjango/add/
> Exception Value: 'float' object is not callable


Please paste your model that is having the problem.

class MyItem(models.Model):
    title=models.CharField(max_length=1024)
    description=models.CharField(max_length=1024)
    price=models.FloatField(default=0.0)
    time=models.DateTimeField()
    name=models.CharField(max_length=256)
    detail=models.CharField(max_length=1024)
    address_city=models.CharField(max_length=36)
    address_area=models.CharField(max_length=36)

    
    def __unicode__(self):
        return self.title

and the ModelAdmin:

class MyItemAdmin(admin.ModelAdmin):
    list_display=('name','title','price','address_city',)

admin.site.register(MyItem, MyItemAdmin)



The traceback indicates that the new object being added is not an instance of MyItem, but rather a simple float. How that has happened is a mystery; it is not due to anything you have posted so far. If you cut-and-paste just the model and model admin definitions you show here into a new 1.3 project, the admin works fine to add items. There is something more to your code that you have not shown yet that is causing the problem. Really, Django can insert float data into a MySQL table (although as Andy notes, using a float field for something named "price" is likely really really really not what you want to be doing.

Karen 
--
http://tracey.org/kmt/

Korobase

unread,
May 19, 2011, 12:45:04 PM5/19/11
to django...@googlegroups.com
I change the floatField to DecimalField and the error also out show:
And from django debug information I can't location which field resulting this error!! 

TypeError at /admin/main/myitem/add/


'Decimal' object is not callable
Request Method:POST
Request URL:http://localhost:8000/admin/main/myitem/add/
Django Version:1.3
Exception Type:TypeError
Exception Value:

'Decimal' object is not callable
Exception Location:C:\Python26\lib\site-packages\django\contrib\admin\options.py in save_model, line 665
Python Executable:C:\Python26\python.exe
Python Version:2.6.6
Python Path:

['D:\\eclipse\\carveout\\d1d2d3\\src\\d1d2d3',
 'C:\\Python26\\lib\\site-packages\\markdown-2.0.3-py2.6-win32.egg',
 'C:\\Python26\\lib\\site-packages\\html5lib-0.90-py2.6.egg',
 'C:\\Python26\\lib\\site-packages\\cheetah-2.4.4-py2.6-win32.egg',
 'C:\\Python26\\lib\\site-packages\\tenjin-1.0.2-py2.6.egg',
 'C:\\Python26\\lib\\site-packages\\tornado-1.2.1-py2.6.egg',
 'C:\\Python26\\lib\\site-packages\\transifex-1.0.0-py2.6.egg',
 'C:\\Python26\\lib\\site-packages\\python_magic-0.4.0-py2.6.egg',
 'C:\\Python26\\lib\\site-packages\\pygooglechart-0.3.0-py2.6.egg',
 'C:\\Python26\\lib\\site-packages\\django_addons-0.6.4-py2.6.egg',
 'C:\\Python26\\lib\\site-packages\\feedparser-5.0.1-py2.6.egg',
 'C:\\WINDOWS\\system32\\python26.zip',
 'C:\\Python26\\DLLs',
 'C:\\Python26\\lib',
 'C:\\Python26\\lib\\plat-win',
 'C:\\Python26\\lib\\lib-tk',
 'C:\\Python26',
 'C:\\Python26\\lib\\site-packages',
 'C:\\Python26\\lib\\site-packages\\PIL']
Server time:Fri, 20 May 2011 00:32:12 +0800

2011/5/15 Karen Tracey <kmtr...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--

Karen Tracey

unread,
May 19, 2011, 9:05:47 PM5/19/11
to django...@googlegroups.com
The error is occurring on the same line of code as it was before, only now it's reporting that a decimal isn't callable where before it was saying that a float isn't callable. So whatever was causing that problem is still afflicting the project, changing float to decimal didn't fix that. I don't know what it is because I don't recall anyone reporting this kind of error before and all you've posted of your code is your model and model admin definitions. If you take just those definitions and cut-and-paste them into a new project, they work fine. So I suspect there is something beyond what you have posted that is causing this.

I'd start with what I just said: take that code you posted and verify that if you cut-and-paste it into a new test project, it works. Then start adding bits from the project you have that shows the weird error and see when it breaks. If it isn't immediately obvious then why that newly-added bit causes the error, if you post the code in question here someone will likely be able to help with the why of it.

Karen
--
http://tracey.org/kmt/

Korobase

unread,
May 20, 2011, 7:31:25 AM5/20/11
to django...@googlegroups.com
OK,Thank you for your reply.

I have resolved this problem, That a fileld name in my models.py is "save" resulting this problem.

But I don't know why. the "save" can't be used as the filed name? because it's not within Python keywords and just be a method name in django.

I 'll keep it in mind.

Thanks to all.

2011/5/20 Karen Tracey <kmtr...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
"OpenBookProject"-开放图书计划邮件列表
详情: http://groups.google.com/group/OpenBookProject
维基: http://wiki.woodpecker.org.cn/
Reply all
Reply to author
Forward
0 new messages