在我的项目中,我尝试使用外部 .py 文件来使用我的 django 应用程序风格来管理数据,如下所示:
在我的 django 项目中,我创建了一个模型:
class temp_test_keywords(models.Model):
main_id = models.ForeignKey(temp_main)
test_id = models.ForeignKey(temp_case)
key_id = models.ForeignKey(temp_keywords)
variable_id = models.ForeignKey(temp_variables, null=True, blank=True)
def __str__(self):
return '%s -> %s' % (str(self.main_id), str(self.test_id))
好吧,现在在我的外部 rst.py 文件中,我像这样启动 django env:
import sys
import os
import django
sys.path.append('core')
os.environ['DJANGO_SETTINGS_MODULE'] = 'core.settings'
django.setup()
好的,此时我导入表并创建类来进行一些思考:
from django.db import models
from django.contrib.contenttypes.fields import
GenericForeignKey,GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.db.models import Count
from frontend.models import temp_test_keywords
class PrepareRst:
def __init__(self,test_id,t_type,log=False):
self.rst = self.mainprep(test_id,t_type)
def mainprep(self,test_id,t_type):
return self.tc_prep(test_id)
#TestCase rst prep method
def tc_prep(self,test_id):
maxpar = temp_test_keywords.objects.filter(main_id = test_id).values('key_id').annotate(total=Count('variable_id')).order_by('-total').first()
totpar = maxpar['total']
#Part1 list creation
count = 0
ltouple = ()
l1 = ["Test Case"]
while (count < totpar):
l1.append("")
count += 1
ltouple += (l1,)
#Query for extract keywords, values
kv = temp_test_keywords.select_related()
但是当我运行 AttributeError: type object 'temp_test_keywords' has no attribute 'select_lated' 错误时引发
如果我从终端启动 python manage.py shell,“kv = temp_test_keywords.select_lated()”命令可以正常工作,为什么在我的 .py 代码中却不能?
提前致谢
最佳答案
尝试,
kv = temp_test_keywords.objects.all().select_related()
关于python - Django ORM select_相关属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44494988/