Django模型在错误的列中返回值

时间:2013-06-23 18:02:25

标签: django sqlite django-models

这很奇怪。

我创建了一个django模型的实例并将其保存到数据库中,但是当我再次回想起它时,值是在错误的列中。我尝试将代码移到另一台机器上,以为我可能有一些库配置错误,但它也失败了。我做了一些广泛的谷歌搜索,但没有发现任何类似于我的问题。

以下是模型定义:(定义__init__,如果重要的话)

class Buffer(models.Model):
    b_bname = models.CharField(max_length=60)
    b_tdate = models.DateField() # date of transaction
    b_bdate = models.DateField() # date at bank
    b_bamnt = models.FloatField()
    b_descript = models.CharField(max_length=120)
    b_kind = models.CharField(max_length=12)
    b_account = models.CharField(max_length=40)

    def __init__(self, name, *args, **kwargs):
        super(Buffer, self).__init__(*args, **kwargs)
        self.b_bname = name

以下是失败的一个例子:

% python manage.py shell
Python 2.7.3 (default, Aug  4 2012, 18:36:39) 
[GCC 4.2.1 20070831 patched [FreeBSD]] on freebsd9
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from buffer.models import Buffer
>>> b = Buffer('schwab')
>>> b.b_tdate = '2008-11-03'
>>> b.b_bdate = '2009-01-20'
>>> b.b_bamnt = 3.1415
>>> b.b_descript = 'easy as pi'
>>> b.b_kind = 'POS'
>>> b.b_account = 'groceries'
>>> b.save()
(0.002) INSERT INTO "buffer_buffer" ("b_bname", "b_tdate", "b_bdate", "b_bamnt", "b_descript", "b_kind", "b_account") VALUES (schwab, 2008-11-03, 2009-01-20, 3.1415, easy as pi, POS, groceries); args=['schwab', u'2008-11-03', u'2009-01-20', 3.1415, 'easy as pi', 'POS', 'groceries']
(0.002) INSERT INTO "buffer_buffer" ("b_bname", "b_tdate", "b_bdate", "b_bamnt", "b_descript", "b_kind", "b_account") VALUES (schwab, 2008-11-03, 2009-01-20, 3.1415, easy as pi, POS, groceries); args=['schwab', u'2008-11-03', u'2009-01-20', 3.1415, 'easy as pi', 'POS', 'groceries']
>>> b.pk                  
1
>>> c = Buffer.objects.get(pk=1)
(0.000) SELECT "buffer_buffer"."id", "buffer_buffer"."b_bname", "buffer_buffer"."b_tdate", "buffer_buffer"."b_bdate", "buffer_buffer"."b_bamnt", "buffer_buffer"."b_descript", "buffer_buffer"."b_kind", "buffer_buffer"."b_account" FROM "buffer_buffer" WHERE "buffer_buffer"."id" = 1 ; args=(1,)
(0.000) SELECT "buffer_buffer"."id", "buffer_buffer"."b_bname", "buffer_buffer"."b_tdate", "buffer_buffer"."b_bdate", "buffer_buffer"."b_bamnt", "buffer_buffer"."b_descript", "buffer_buffer"."b_kind", "buffer_buffer"."b_account" FROM "buffer_buffer" WHERE "buffer_buffer"."id" = 1 ; args=(1,)
>>> c.b_bname
1
>>> c.b_tdate
datetime.date(2009, 1, 20)
>>> c.b_bdate
3.1415
>>> c.b_bamnt
u'easy as pi'
>>> c.b_kind
u'groceries'
>>> c.b_account
''
>>> 
% sqlite3 register 
SQLite version 3.7.13 2012-06-11 02:05:22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> select count(id) from buffer_buffer;
1
sqlite> select b_bamnt from buffer_buffer;
3.1415

如您所见,数据库是正确的,但检索到的对象是错误的。我搞砸了哪里?我必须在某个地方有一个bug。我现在已经打了一天多了。

环境:Python 2.7,Django 1.4,sqlite 3.7作为后端,FreeBSD 9.0

1 个答案:

答案 0 :(得分:3)

请勿更改模型的__init__方法的类型签名以获取name参数。它不仅在shell中创建实例时被调用,而且在初始化实例时也会被调用,包括从db中获取时。

当您可以按如下方式使用命名参数时,不清楚为什么需要覆盖__init__方法:

  
    
      

b =缓冲区(name ='schwab')

    
  

如果您在创建新实例时需要一种便捷方法,请考虑使用create方法创建自定义管理器。例如,查看User.objects.create_user()方法,该方法负责哈希密码。