2

In my project im trying to use an external .py file for manage data using my django app style like this:

In my django project i create a model:

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))

Well, now in my external rst.py file i start django env like this:

import sys
import os
import django

sys.path.append('core')
os.environ['DJANGO_SETTINGS_MODULE'] = 'core.settings'
django.setup()

ok, at this point i import table and create class for do some thinks with it:

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()

but when i run an AttributeError: type object 'temp_test_keywords' has no attribute 'select_related' error raise

if i start python manage.py shell from terminal the "kv = temp_test_keywords.select_related()" command works fine, why in my .py code doesn't?

Thanks in advance

1
  • Try temp_test_keywords.objects.select_related('SomeAttr') Commented Jun 12, 2017 at 8:47

1 Answer 1

1

Try,

kv = temp_test_keywords.objects.all().select_related()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.