python - Django ORM select_相关属性错误

标签 python django python-2.7 orm attributeerror

在我的项目中,我尝试使用外部 .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/

相关文章:

python - 如何在python中的特定字符后拆分字符串

python - 将控制台日志输出重定向到flask-socketio

javascript - 如何在 JavaScript 文件中使用 Django 变量?

django - 如何使用 django-cumulus 提供静态文件?

python - 由于文件名而无法导入文件

python - python private-attribute_class 中的错误

python - 将音高转换为单声道 MIDI

python - 如何使用 python pandas 降低时间复杂度或提高程序查找月份间隙的效率

django - 在 Django Rest 框架中出现特定异常时如何更改响应中收到的状态代码

Python Tkinter : Call function after long press spacebar for 1 second