如何在Django中获取外键关系数组

时间:2018-09-10 08:25:05

标签: django python-3.x

我有一个带有外键country_id的国家表及其翻译表,一个带有外键country_id的城市表,以及带有外键city_id的城市翻译表。 所以我的model.py就是这样。

from django.db import models

# Create your models here.


class Country(models.Model):
    #id             = models.IntegerField(primary_key=True)
    iso_code    = models.CharField(max_length=2, unique=True)
    slug        = models.CharField(max_length=255, unique=True)
    is_featured = models.IntegerField(max_length=1)

    class Meta:
        db_table = 'rh_countries'

class CountryTranslation(models.Model):
    country     = models.ForeignKey(Country, on_delete=models.CASCADE)
    name        = models.CharField(max_length=255)
    description = models.TextField(blank=True, null=True)
    locale      = models.CharField(max_length=2)

    class Meta:
        db_table = 'rh_countries_translations'

class City(models.Model):
    country     = models.ForeignKey(Country, on_delete=models.CASCADE)

    class Meta:
        db_table = 'rh_cities'

class CityTranslation(models.Model):
    city        = models.ForeignKey(City, on_delete=models.CASCADE)
    name        = models.CharField(max_length=255)
    description = models.TextField(blank=True, null=True)
    locale      = models.CharField(max_length=2)

    class Meta:
        db_table = 'rh_city_translations'

还有我的views.py文件。

from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse
from django.template import loader
from home.models import Country, CountryTranslation, City, CityTranslation
from django.db.models import F, Count

def index(request):
    #return HttpResponse("Hello, world. You're at the polls index.")
    template = loader.get_template('home/index.html')
    context = {
        "countries" : Country.objects.filter(
                            is_featured=1,
                            countrytranslation__locale='en'
                        ).annotate(
                            name=F('countrytranslation__name'),
                            totalCities=Count('city', distinct=True)
                        )
    }
    return render(request, 'home/index.html', context)

这样,我得到了该国的全部城市。但是我想要列出具有语言环境en的所有国家的3个城市的名称。 请帮助我以这种方式检索数据。 样本输出应为:

Array{
    0 => {
        id => 1
        name => 'Australia',
        is_featured => 1,
        totalCities => 5,
        cities => {
            0 => {
                id => 1,
                name => 'Sydney',
                locale => 'en'
            },
            1 => {
                id => 2,
                name => 'Melbourne',
                locale => 'en'
            },
            2 => {
                id => 3,
                name => 'Perth',
                locale => 'en'
            }
        }
    },
    2 => {
        id => 2
        name => 'Spain',
        is_featured => 1,
        totalCities => 12,
        cities => {
            0 => {
                id => 9,
                name => 'Madrid',
                locale => 'en'
            },
            1 => {
                id => 10,
                name => 'Bercelona',
                locale => 'en'
            },
            2 => {
                id => 11,
                name => 'Valencia',
                locale => 'en'
            }
        }
    }
}

0 个答案:

没有答案