为什么这个查询集过滤器会以静默方式崩溃?

时间:2012-05-21 11:34:57

标签: django django-models

基本上,我要做的是在查询中过滤掉一家公司,而这段代码是很多调试的结果。我们已经将它简化为尽可能简单,并且无法解决为什么“为公司在Company.objects.filter(pk =”E0433CAE-1756-11E1-A97B-7071BC08AB6F“):”失败。在其他地方,它完美地工作:s

Autovivification,自动嵌套字典。见Best way to automatically nest dictionaries

要运行的代码:

def run_code(code, query_dict):
    code = code.replace('\r\n', '\n')
    import jmsdirectory.advert.models
    import jmsdirectory.boughtin.models
    import jmsdirectory.contacts.models
    import jmsdirectory.generic.models
    import jmsdirectory.joinerysoft.models
    import jmsdirectory.scheduler.models
    import jmsdirectory.suppliers.models
    # g = {'__builtins__': False }
    # try:
    print "WOOPS"
    # print g
    print query_dict
    class AutoVivification(dict):
        print "AutoVivification: 1"
        """Implementation of perl's autovivification feature."""
        def __getitem__(self, item):
            try:
                return dict.__getitem__(self, item)
            except KeyError:
                value = self[item] = type(self)()
                return value
    print "AutoVivification: 69"
    models = AutoVivification()
    print "AutoVivification: 71"

    for company in Company.objects.filter(pk="E0433CAE-1756-11E1-A97B-7071BC08AB6F"):
        print "AutoVivification: 74"
        for address in Address.objects.filter(company=query_dict['company']).exclude(allow_download=0):
            print "AutoVivification: 76"
            for contact in Contact.objects.filter(address=address.uuid).exclude(allow_download=0):
                print "AutoVivification: 78"
                for supplier in Supplier.objects.filter(company=query_dict['company']):
                    print "AutoVivification: 80"
                    for supplies in Supplies.objects.filter(supplier=supplier.uuid):
                        print "AutoVivification: 82"
                        models[company][address][contact][supplier] = supplies
                        print "AutoVivification: 84"
                        print models
    print "Rage!"
    result = models
    # exec code in locals()
    print "MEH!"
    # except:
        # print "Error in query set"
        # print traceback.print_exc()
        # return False
    return result

输出:

Django version 1.2.7, using settings 'jmsdirectory.settings'
Development server is running at http://127.0.0.1:8002/
Quit the server with CTRL-BREAK.
[21/May/2012 12:22:18] "POST /transfer/server/ HTTP/1.0" 200 162
[21/May/2012 12:22:18] "POST /transfer/server/ HTTP/1.0" 200 531
[21/May/2012 12:22:18] "POST /transfer/server/ HTTP/1.0" 200 162
1
2
3
4
5
6
7
8
9
WOOPS
{'company': u'E0433CAE-1756-11E1-A97B-7071BC08AB6F', 'contact': u'E047B1A8-1756-11E1-A97B-7071BC08AB6F', 'address': u'E0452488-1756-11E1-A97B-7071BC08AB6F'}
AutoVivification: 1
AutoVivification: 69
AutoVivification: 71
[21/May/2012 12:22:18] "POST /transfer/server/ HTTP/1.0" 200 129

公司模型:

class Company(models.Model):
    uuid = UUIDField(primary_key=True)
    name =  models.CharField(null=True, blank=True,max_length=255,verbose_name=_('Company Name'))
    internal_name = models.CharField(null=True, blank=True,max_length=255,verbose_name=_('Internal Name'))
    reference = models.CharField(null=True, blank=True,max_length=255,verbose_name=_('Reference'))
    company_status = models.ForeignKey(CompanyStatus, null=True, db_column='company_status_uuid',verbose_name=(_('Company Status')), serialize=False)
    vat_number = models.CharField(null=True, blank=True,max_length=255,verbose_name=(_('Vat Number')))
    registration_number = models.CharField(null=True, blank=True,max_length=255,verbose_name=(_('Company Number')))
    discount = models.FloatField(null=True, blank=True)
    notes = models.TextField(null=True, blank=True,max_length=255)
    jms_code = models.TextField(null=True, blank=True,max_length=255)
    logo = models.TextField(null=True, blank=True,max_length=255)
    date_created = models.DateTimeField(null=True, blank=True, auto_now_add=True,verbose_name=_('Date Time'), serialize=False)
    date_modified = models.DateTimeField(null=True, blank=True, auto_now=True,verbose_name=_('Date Time Updated'), serialize=False)
    hidden = models.NullBooleanField(null=True, blank=True,default=0, serialize=False)
    user = UserField(null=True, blank=True, serialize=False)
    allow_download = models.NullBooleanField()
    is_modified = ModifiedField(serialize=False)
    companyid = models.IntegerField(null=True, blank=True, serialize=False)
    seqno = models.IntegerField(null=True, blank=True, serialize=False)

    def __unicode__(self):
        return self.name

    def advertiser_uuid(self):
        uuid = self.uuid
        today = date.today()

        from jmsdirectory.advert.models import Advertiser
        a=Advertiser.objects.filter(company=self,start_date__lte=today,end_date__gte=today)
        if len(a) > 0:
            return a[0].uuid
        else:
            return ''

    def advertiser(self):
        if self.advertiser_uuid() != '':
            return True
        else:
            return False

    def get_sequence (self) :

        if self.seqno is None :
            from django.db.models import Max
            max = Company.objects.all().aggregate(Max('seqno'))['seqno__max']
            if max is None :
                   self.companyid = 1
            else : self.companyid = max + 1
            self.seqno = 1
        else :
            self.seqno += 1

        self.save()
        return self.companyid, self.seqno

    class Meta:
        verbose_name_plural = ('Companies')
        ordering = ('name',)
        db_table = 'company'

修正:

def run_code(code, query_dict):
    code = code.replace('\r\n', '\n')

    class AutoVivification(dict):
        """Implementation of perl's autovivification feature."""
        def __getitem__(self, item):
            try:
                return dict.__getitem__(self, item)
            except KeyError:
                value = self[item] = type(self)()
                return value

    models = AutoVivification()


    companies = Company.objects.filter(pk="E0433CAE-1756-11E1-A97B-7071BC08AB6F")
    addresses = Address.objects.filter(company=query_dict['company']).exclude(allow_download=0)
    suppliers = Supplier.objects.filter(company=query_dict['company'])
    for company in companies:
        for address in addresses:
            for contact in Contact.objects.filter(address=address.uuid).exclude(allow_download=0):
                for supplier in suppliers:
                    for supply in Supplies.objects.filter(supplier=supplier.uuid):
                        models[company][address][contact][supplier] = supply
    result = models

    return result

1 个答案:

答案 0 :(得分:2)

简短的回答是 - 它没有崩溃,它只是没有返回任何东西,所以你在迭代空集,因此没有任何东西被打印。

答案很长:

我会尽力帮助你,但我的第一个建议是将问题简化为基础。例如,使用defaultdict代替您的autovivi类。

您还可以看到其他一些问题,例如:

for address in Address.objects.filter(company=query_dict['company'])

您正试图针对字符串过滤PK,我想您的意思是:

for address in Address.objects.filter(company__uuid=query_dict['company'])

根据您的其他模型判断,我怀疑您的初始查询应该是:

for company in Company.objects.filter(uuid="E0433CAE-1756-11E1-A97B-7071BC08AB6F")

同样,您应该修复其他查询:

contact in Contact.objects.filter(address__uuid=address.uuid)或更好 contact in Contact.objects.filter(address=address)如果您有适当的FK关系。

但是,正如丹尼尔暗示的那样,即使你实现这一目标,这也是非常低效的。您应该利用filter chainingforeign key lookups