基本的に、私がやろうとしているのは、クエリ内の会社を除外することです。このコードは、多くのデバッグの結果です。できるだけ単純にまとめましたが、「for company in Company.objects.filter(pk="E0433CAE-1756-11E1-A97B-7071BC08AB6F"):" が失敗する理由がわかりません。他の場所でも完璧に動作します:s
Autovivification は、辞書を自動的にネストします。辞書を自動的にネストする最良の方法を参照してください
実行するコード:
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