当前位置: 首页 > 知识库问答 >
问题:

为什么我不能在django/sqlite中更改我的数据库对象?

刘选
2023-03-14

当我试图在Django管理面板中查看我的产品时,出现以下错误。在我的产品模型中,我有“models.DecimalField”,我认为这很重要。Django错误描述

base.py第34行是一个函数:

def decoder(conv_func):

"""
Convert bytestrings from Python's sqlite3 interface to a regular string.
"""
return lambda s: conv_func(s.decode())

更多信息:无效操作at/admin/products/products/



Request Method: GET
Request URL: http://127.0.0.1:8000/admin/products/products/

Django Version: 2.0.7
Python Version: 3.5.2
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'products',
 'pages']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/home/rt7/Pulpit/django_yt/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/home/rt7/Pulpit/django_yt/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "/home/rt7/Pulpit/django_yt/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/rt7/Pulpit/django_yt/lib/python3.5/site-packages/django/contrib/admin/options.py" in wrapper
  575.                 return self.admin_site.admin_view(view)(*args, **kwargs)

File "/home/rt7/Pulpit/django_yt/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "/home/rt7/Pulpit/django_yt/lib/python3.5/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs)

File "/home/rt7/Pulpit/django_yt/lib/python3.5/site-packages/django/contrib/admin/sites.py" in inner
  223.             return view(request, *args, **kwargs)

File "/home/rt7/Pulpit/django_yt/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapper
  62.             return bound_func(*args, **kwargs)

File "/home/rt7/Pulpit/django_yt/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "/home/rt7/Pulpit/django_yt/lib/python3.5/site-packages/django/utils/decorators.py" in bound_func
  58.                 return func.__get__(self, type(self))(*args2, **kwargs2)

File "/home/rt7/Pulpit/django_yt/lib/python3.5/site-packages/django/contrib/admin/options.py" in changelist_view
  1708.             selection_note=_('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)},

File "/home/rt7/Pulpit/django_yt/lib/python3.5/site-packages/django/db/models/query.py" in __len__
  254.         self._fetch_all()

File "/home/rt7/Pulpit/django_yt/lib/python3.5/site-packages/django/db/models/query.py" in _fetch_all
  1179.             self._result_cache = list(self._iterable_class(self))

File "/home/rt7/Pulpit/django_yt/lib/python3.5/site-packages/django/db/models/query.py" in __iter__
  53.         results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)

File "/home/rt7/Pulpit/django_yt/lib/python3.5/site-packages/django/db/models/sql/compiler.py" in execute_sql
  1101.                 return list(result)

File "/home/rt7/Pulpit/django_yt/lib/python3.5/site-packages/django/db/models/sql/compiler.py" in cursor_iter
  1462.         for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):

File "/home/rt7/Pulpit/django_yt/lib/python3.5/site-packages/django/db/models/sql/compiler.py" in <lambda>
  1462.         for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):

File "/home/rt7/Pulpit/django_yt/lib/python3.5/site-packages/django/db/utils.py" in inner
  96.                 return func(*args, **kwargs)

File "/home/rt7/Pulpit/django_yt/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py" in <lambda>
  34.     return lambda s: conv_func(s.decode())

Exception Type: InvalidOperation at /admin/products/products/
Exception Value: [<class 'decimal.ConversionSyntax'>]

产品/admin.py

来自django。contrib导入管理

从…起进口产品模型

管理地点登记册(产品)

共有1个答案

周泰
2023-03-14

我发现了我的错误。在我的模型的第一个版本中,我添加了以价格作为字符串值的产品。

class Products(models.Model):
    title       = models.CharField(max_length=120)
    description = models.TextField()
    price       = models.CharField(max_length=120)

然后我更改了这个类,并将price值设置为DecimalField()

class Products(models.Model):
    title       = models.CharField(max_length=120)
    description = models.TextField()
    price       = models.DecimalField(null=False, decimal_places=0, max_digits=1000)

在我的例子中,这很容易解决,因为我只添加了一个以价格为字符串的产品。我使用了“SQLite数据库浏览器”,并手动更改了值。

 类似资料:
  • 我想从sqlite数据库添加的数据中在listview中显示我的所有数据。当我单击save按钮时,它显示一条消息“added successful”。但每次它都显示错误消息“data not found”。为什么不显示listview? 错误显示如下: MyDatabaseHelper.java

  • 我正在尝试使用python和MySQL开发药房管理系统。但我在连接数据库时遇到了问题。 下面是一些代码:

  • 目前,我正在尝试在这里实现汽车示例的变体: https://www.elastic.co/blog/managing-relations-inside-elasticsearch 如果我运行: 代码工作正常。 但是如果我删除索引并将2015从字符串更改为数字: 我收到以下错误消息: {“error”:{“root\u-cause”:[{“type”:“非法\u-argument\u-excepti

  • 问题内容: 我能够从PHP中的SQLite数据库中检索信息,但无法写入信息。例如,此代码可以正常工作: 但是,以下代码段导致“无法打开数据库文件”错误: 当我从命令行而不是在PHP中执行上述INSERT时,我得到了预期的结果(tblname中的新行): 这是我第一次使用PDO(或SQLite),因此对您的帮助将不胜感激。 问题答案: 检查运行PHP脚本的用户是否具有写foo.db的权限。 同样(从

  • 基本上,我试图用一个名为“a”的变量设置数组的长度,并将其声明为3和int。当我试图用for循环查看所有元素时,我不会将“null”作为元素。到现在为止,一直都还不错。然而,当有人想向该数组中添加一个元素时,我会向提到的变量“a”中添加1,从而延长该数组的长度,并允许我向该数组中再添加一个元素。但它不起作用。以下是代码: }