python - Getting other tables Django filter/select_related -
i trying filter values using django's filter , select_related. able base item, can't of joined fields.
item = items.objects.filter(tests__company_user_id__user_id=user_id).filter(item_id=item_id).select_related("tests__company_user").values()
my goal data user associated item onto item return. possible?
the data model looks this:
class items(models.model): item_id = models.charfield(primary_key=true,max_length=100) mailed_date = models.datetimefield() received_date = models.datetimefield() last_viewed = models.datetimefield() dateitemsent = models.datetimefield() itemsent = models.charfield(max_length=25) itemret = models.charfield(max_length=25) dateitemret = models.datetimefield() status = models.charfield(max_length=25) class tests(models.model): tests_id = models.charfield(primary_key=true, unique=true, max_length=100) teststypetest_id = models.foreignkey(teststype) dateitemord = models.datetimefield() testdate = models.datetimefield() teststypetest_id = models.foreignkey(teststype) dateitemord = models.datetimefield() testdate = models.datetimefield() status = models.charfield(max_length=100) item_id = models.foreignkey(kits) company_user_id = models.foreignkey(companyuser) class companyuser(models.model): user_id = models.charfield(primary_key=true,max_length=100) username = models.charfield(max_length=254,default="empty") sec_question_1 = models.charfield(max_length=254) sec_answer_1 = models.charfield(max_length=254) sec_question_2 = models.charfield(max_length=254) sec_answer_2 = models.charfield(max_length=254) sec_question_3 = models.charfield(max_length=254) sec_answer_3 = models.charfield(max_length=254)
when use select_related() caching joined object info not retrieving, need explicitly pull data:
item = items.objects.filter(tests__company_user_id__user_id=user_id).filter(item_id=item_id).select_related("tests__company_user").values("tests__company_user_id__sec_question_1", "tests__company_user_id__sec_question_2", ...)
edit
let's suppose want 2 fields companyuser
: sec_question_1
, sec_question_2
:
from django.db.models import f item = items.objects.filter(tests__company_user_id__user_id=user_id).filter(item_id=item_id).select_related("tests__company_user").values().annotate(q1=f('tests__company_user_id__sec_question_1'), q2=f('tests__company_user_id__sec_question_2'))
Comments
Post a Comment