2013-10-26 40 views
0

我有一个django应用程序,它一直工作正常,突然间有些事似乎被打破了。Django Shell和Django Web App给出了不同的结果

class ClosedUserGroup(models.Model): 
    """ Preset definitions for ClosedUserGroup: 
    """ 
    is_active = models.BooleanField(default=True) 
    is_deleted = models.BooleanField(default=False) 
    created_at = models.DateTimeField(
     auto_now=True, 
     auto_now_add=True, 
     null=True, 
     default='2013-08-15 13:37:13.370030' 
    ) 
    name = models.CharField(max_length=256, default='<MISSING:CUG_NAME') 
    description = models.CharField(max_length=256) 


class Partner(models.Model): 
    id = models.IntegerField(primary_key=True) 
    is_active = models.BooleanField(default=True) 
    is_deleted = models.BooleanField(default=False) 
    created_at = models.DateTimeField(
     auto_now=True, 
     auto_now_add=True, 
     null=True, 
     default=datetime.datetime.now() 
    ) 
    name = models.CharField(max_length=256, default='<MISSING:PARTNERNAME>') 

class PartnerCug(models.Model): 
    """ Partner to ClosedUserGroup relation 
    """ 
    is_active = models.BooleanField(default=True) 
    is_deleted = models.BooleanField(default=False) 
    created_at = models.DateTimeField(
     auto_now=True, 
     auto_now_add=True, 
     null=True, 
     default='2013-08-15 13:37:13.370030' 
    ) 
    partner = models.ForeignKey(Partner) 
    cug = models.ForeignKey(ClosedUserGroup)   

class Account(models.Model): 
    """ 
     Account: 
     TODO:DESCRIPTION 
    """ 
    id = models.IntegerField(primary_key=True) 
    is_active = models.BooleanField(default=True) 
    is_deleted = models.BooleanField(default=False) 
    created_at = models.DateTimeField(
     auto_now=True, 
     auto_now_add=True, 
     null=True, 
     default=datetime.datetime.now() 
    ) 
    number = models.CharField(max_length=64) 
    submitted_by = models.ForeignKey(
     Partner, 
     db_column='submitted_by' 
    )  

在Django壳,下面的命令成功运行,并返回结果1:

from myapp.models import Account 
account_object = Account.objects.filter(id=1) 
account_object.filter(account__submitted_by__partnercug__cug=3).count() 

然而,在Django本身,我具备的功能

def get_other_accounts(): 
    """get total accounts""" 
    account_object = Account.objects.filter(id=1) 
    total_accounts = account_object.filter(account__submitted_by__partnercug__cug=3).count() 

然而,这失败错误号为DatabaseError: current transaction is aborted, commands ignored until end of transaction block,它指向该函数的最后一行。所以它的工作Django壳,但不是从Web应用程序。

+0

退出shell后试过了吗? – Rohan

+0

看起来你在两个过滤器中得到了什么,它没有什么不同? – drabo2005

+0

你知道在shell中你正在计算'PartnerCug'对象,并且在Django函数中,你正在计算'ClosedUserGroup'(cug)对象,无论这些对象是什么? – frnhr

回答

0

缺失idpk字段(主键)ClosedUserGroup型号?

我看不出如何在shell或web中工作。

+0

该模型没有,但夹具有这样的数据库中的主要ID在那里。 – lukik

+0

将它添加到模型呢? Django不应该知道任何没有被Field描述的DB列,它们只会导致问题 – frnhr

相关问题