Django-创建关联对象的字典

时间:2018-09-15 23:53:25

标签: python django python-3.x

我正在尝试建立一个查询,该查询将返回所有技术的列表以及与每种技术相关的每个用户的得分。这基于以下模型:

class Technology(models.Model):
    technology = models.CharField(max_length=64)

class Result(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    technology = models.ForeignKey(technology, on_delete=models.CASCADE)
    skill_level = models.ForeignKey(skill, to_field='level', on_delete=models.CASCADE)

class Skill(models.Model):
    level = models.IntegerField(unique=True)

基本上,我希望看到类似以下内容的东西:

{技术:思科,用户:{Alex:{skill_level:1}},{James:{skill_level:2}}

我不确定是否可以通过简单的查询来实现,还是不确定是否需要执行自定义联接?

我也不确定是否应该在Technology模型还是Result模型上构建它?

1 个答案:

答案 0 :(得分:1)

可能与此类似(请参见tests.py文件底部):

documentation

models.py:

from django.db import models

class User(models.Model):
    name = models.CharField(max_length=64)

class Technology(models.Model):
    technology = models.CharField(max_length=64)

class Skill(models.Model):
    level = models.IntegerField(unique=True)

class Result(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    technology = models.ForeignKey(Technology, on_delete=models.CASCADE)
    skill_level = models.ForeignKey(Skill, to_field='level', on_delete=models.CASCADE)

tests.py:

from django.test import TestCase
from .models import Result, Technology, User, Skill
from django.forms.models import model_to_dict

class SimpleTest(TestCase):
    def test_join(self):
        u = []
        s = []
        for x in range(0,2):
            _u = User(name='User{}'.format(str(x)))
            _s = Skill(level=x)
            _u.save()
            _s.save()
            u.append(_u)
            s.append(_s)
        t = []
        for x in range(0, 4):
            _t = Technology(technology='blah{}'.format(str(x)))
            _t.save()
            t.append(_t)
        for x in range(0, 8):
            r = Result(user=u[x%2], technology=t[x%4], skill_level=s[x%2])
            r.save()

        x = Technology.objects.select_related()
        print(x.query)
        for item in x:
            print(item.result_set.values())
            print(item.result_set.all().query)
            for ok in item.result_set.all():
                print(model_to_dict(ok))

测试输出:

➜  mysite python manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
SELECT "mysite_technology"."id", "mysite_technology"."technology" FROM "mysite_technology"
<QuerySet [{'id': 1, 'user_id': 1, 'technology_id': 1, 'skill_level_id': 0}, {'id': 5, 'user_id': 1, 'technology_id': 1, 'skill_level_id': 0}]>
SELECT "mysite_result"."id", "mysite_result"."user_id", "mysite_result"."technology_id", "mysite_result"."skill_level_id" FROM "mysite_result" WHERE "mysite_result"."technology_id" = 1
{'id': 1, 'user': 1, 'technology': 1, 'skill_level': 0}
{'id': 5, 'user': 1, 'technology': 1, 'skill_level': 0}
<QuerySet [{'id': 2, 'user_id': 2, 'technology_id': 2, 'skill_level_id': 1}, {'id': 6, 'user_id': 2, 'technology_id': 2, 'skill_level_id': 1}]>
SELECT "mysite_result"."id", "mysite_result"."user_id", "mysite_result"."technology_id", "mysite_result"."skill_level_id" FROM "mysite_result" WHERE "mysite_result"."technology_id" = 2
{'id': 2, 'user': 2, 'technology': 2, 'skill_level': 1}
{'id': 6, 'user': 2, 'technology': 2, 'skill_level': 1}
<QuerySet [{'id': 3, 'user_id': 1, 'technology_id': 3, 'skill_level_id': 0}, {'id': 7, 'user_id': 1, 'technology_id': 3, 'skill_level_id': 0}]>
SELECT "mysite_result"."id", "mysite_result"."user_id", "mysite_result"."technology_id", "mysite_result"."skill_level_id" FROM "mysite_result" WHERE "mysite_result"."technology_id" = 3
{'id': 3, 'user': 1, 'technology': 3, 'skill_level': 0}
{'id': 7, 'user': 1, 'technology': 3, 'skill_level': 0}
<QuerySet [{'id': 4, 'user_id': 2, 'technology_id': 4, 'skill_level_id': 1}, {'id': 8, 'user_id': 2, 'technology_id': 4, 'skill_level_id': 1}]>
SELECT "mysite_result"."id", "mysite_result"."user_id", "mysite_result"."technology_id", "mysite_result"."skill_level_id" FROM "mysite_result" WHERE "mysite_result"."technology_id" = 4
{'id': 4, 'user': 2, 'technology': 4, 'skill_level': 1}
{'id': 8, 'user': 2, 'technology': 4, 'skill_level': 1}
.
----------------------------------------------------------------------
Ran 1 test in 0.011s

OK
Destroying test database for alias 'default'...

相关: