Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用外键从django中的2层深度关系获取关联表数据_Django_Django Models - Fatal编程技术网

使用外键从django中的2层深度关系获取关联表数据

使用外键从django中的2层深度关系获取关联表数据,django,django-models,Django,Django Models,这是我的问题,我试图找出作为一个django的初学者 我想在用户应用程序中获取国家,知道城市表使用外键连接到国家表。用户表具有city\u id列。请注意,用户已登录。我已经得到了城市,但没有得到乡村 之后,在我的讲师应用程序中,我得到了连接到国家/地区列currency\u id的货币 这就是我所拥有的 /讲师/model.py from django.db import models from locations.models import City, Country class Clas

这是我的问题,我试图找出作为一个django的初学者

  • 我想在
    用户
    应用程序中获取国家,知道城市表使用外键连接到国家表。用户表具有city\u id列。请注意,用户已登录。我已经得到了城市,但没有得到乡村

  • 之后,在我的
    讲师
    应用程序中,我得到了连接到国家/地区列currency\u id的货币

  • 这就是我所拥有的

    /讲师/model.py

    from django.db import models
    from locations.models import City, Country
    
    class Class(models.Model):
       ...
       city = models.ForeignKey(City, null=True, blank=True)
    
    
    class Currency(models.Model):
        name = models.CharField(max_length=20)
        symbol = models.CharField(max_length=7, help_text="Code HTML")
        initials = models.CharField(max_length=5, help_text="e.g. : CLP (Chilean Pesos)")
    
        class Meta:
            verbose_name_plural = "currencies"
    
        def __unicode__(self):
            return self.name
    
        def get_label(self):
            return '%s (%s)' % (self.symbol, self.initials)
    
    class User(AbstractUser):
        ...
        city = models.ForeignKey(City, blank=True, null=True, db_index=True)
        ...
    
        def get_locality(self):
            locality = ''
            if self.location:
                locality = '%s, %s, %s' % (self.location.name, self.location.city.name, self.city.country.name)
            elif self.city:
                locality = '%s, %s' % (self.city.name, self.city.country.name)
            return locality
    
    from django.db import models
    
    class Country(models.Model):
        code = models.CharField(max_length=3)
        name = models.CharField(max_length=100)
        order = models.IntegerField(default=0)
        currency_id = models.IntegerField(max_length=3)
    
        class Meta:
            verbose_name_plural = "countries"
    
        def __unicode__(self):
            return self.name
    
    
    class City(models.Model):
        country = models.ForeignKey(Country)
        name = models.CharField(max_length=100)
        order = models.IntegerField(default=0)
    
        class Meta:
            verbose_name_plural = "cities"
    
        def __unicode__(self):
            return "%s, %s" % (self.name, self.country)
    
    
    class Location(models.Model):
        city = models.ForeignKey(City)
        name = models.CharField(max_length=100)
    
        class Meta:
            verbose_name_plural = "locations"
    
        def __unicode__(self):
            return "%s, %s" % (self.name, self.city)
    
    /用户/model.py

    from django.db import models
    from locations.models import City, Country
    
    class Class(models.Model):
       ...
       city = models.ForeignKey(City, null=True, blank=True)
    
    
    class Currency(models.Model):
        name = models.CharField(max_length=20)
        symbol = models.CharField(max_length=7, help_text="Code HTML")
        initials = models.CharField(max_length=5, help_text="e.g. : CLP (Chilean Pesos)")
    
        class Meta:
            verbose_name_plural = "currencies"
    
        def __unicode__(self):
            return self.name
    
        def get_label(self):
            return '%s (%s)' % (self.symbol, self.initials)
    
    class User(AbstractUser):
        ...
        city = models.ForeignKey(City, blank=True, null=True, db_index=True)
        ...
    
        def get_locality(self):
            locality = ''
            if self.location:
                locality = '%s, %s, %s' % (self.location.name, self.location.city.name, self.city.country.name)
            elif self.city:
                locality = '%s, %s' % (self.city.name, self.city.country.name)
            return locality
    
    from django.db import models
    
    class Country(models.Model):
        code = models.CharField(max_length=3)
        name = models.CharField(max_length=100)
        order = models.IntegerField(default=0)
        currency_id = models.IntegerField(max_length=3)
    
        class Meta:
            verbose_name_plural = "countries"
    
        def __unicode__(self):
            return self.name
    
    
    class City(models.Model):
        country = models.ForeignKey(Country)
        name = models.CharField(max_length=100)
        order = models.IntegerField(default=0)
    
        class Meta:
            verbose_name_plural = "cities"
    
        def __unicode__(self):
            return "%s, %s" % (self.name, self.country)
    
    
    class Location(models.Model):
        city = models.ForeignKey(City)
        name = models.CharField(max_length=100)
    
        class Meta:
            verbose_name_plural = "locations"
    
        def __unicode__(self):
            return "%s, %s" % (self.name, self.city)
    
    /位置/model.py

    from django.db import models
    from locations.models import City, Country
    
    class Class(models.Model):
       ...
       city = models.ForeignKey(City, null=True, blank=True)
    
    
    class Currency(models.Model):
        name = models.CharField(max_length=20)
        symbol = models.CharField(max_length=7, help_text="Code HTML")
        initials = models.CharField(max_length=5, help_text="e.g. : CLP (Chilean Pesos)")
    
        class Meta:
            verbose_name_plural = "currencies"
    
        def __unicode__(self):
            return self.name
    
        def get_label(self):
            return '%s (%s)' % (self.symbol, self.initials)
    
    class User(AbstractUser):
        ...
        city = models.ForeignKey(City, blank=True, null=True, db_index=True)
        ...
    
        def get_locality(self):
            locality = ''
            if self.location:
                locality = '%s, %s, %s' % (self.location.name, self.location.city.name, self.city.country.name)
            elif self.city:
                locality = '%s, %s' % (self.city.name, self.city.country.name)
            return locality
    
    from django.db import models
    
    class Country(models.Model):
        code = models.CharField(max_length=3)
        name = models.CharField(max_length=100)
        order = models.IntegerField(default=0)
        currency_id = models.IntegerField(max_length=3)
    
        class Meta:
            verbose_name_plural = "countries"
    
        def __unicode__(self):
            return self.name
    
    
    class City(models.Model):
        country = models.ForeignKey(Country)
        name = models.CharField(max_length=100)
        order = models.IntegerField(default=0)
    
        class Meta:
            verbose_name_plural = "cities"
    
        def __unicode__(self):
            return "%s, %s" % (self.name, self.country)
    
    
    class Location(models.Model):
        city = models.ForeignKey(City)
        name = models.CharField(max_length=100)
    
        class Meta:
            verbose_name_plural = "locations"
    
        def __unicode__(self):
            return "%s, %s" % (self.name, self.city)
    

    我看到您已经在
    get\u locality
    方法中引用了相关对象。你想做什么你不知道怎么做?为用户获取
    货币
    应该类似于
    self.city.country.Currency
    ,根据您在
    国家/地区
    模型上对
    ForeignKey
    字段的命名。实际上,self.location.city.Country.name不会返回预期数据。如果您添加位置模型,我将查看是否有明显错误。
    self.location
    是您的
    User
    模型上的另一个FK字段吗?是的,您是对的,这是另一个FK。您的位置模型看起来都很好-
    self.location.city.country.name
    应该可以工作。它的回报是什么?你试过穿过FK链吗?例如,查看什么是
    self.location
    ,然后查看
    self.location.city
    ,等等。对于您的国家/货币连接,您最好使用从
    country
    currency
    的FK,而不是尝试使用整数ID字段来实现它。