代码之家  ›  专栏  ›  技术社区  ›  Raj

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

  •  0
  • Raj  · 技术社区  · 6 年前

    country_id 国家id ,和带外键的城市翻译表 city_id 所以我的模特就这样。

    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 . 请帮助我这样检索数据。

    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 回复  |  直到 6 年前