如何访问由两个外键组成的django模型中的对象?

时间:2018-01-21 14:36:29

标签: python django django-models django-views

这些是我的应用程序的相关类。我想基本了解某个用户(表单AuthUser)是否通过查看UserBusinessInformation链接到一个企业(来自BusinessInformation)。感谢

class AuthUser(models.Model):
    password = models.CharField(max_length=128)
    last_login = models.DateTimeField(blank=True, null=True)
    is_superuser = models.IntegerField()
    username = models.CharField(unique=True, max_length=150)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    email = models.CharField(max_length=254)
    is_staff = models.IntegerField()
    is_active = models.IntegerField()
    date_joined = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'auth_user'

class BusinessInformation(models.Model):
    business_id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255)
    lat = models.CharField(max_length=255)
    lng = models.CharField(max_length=255)
    formatted_address = models.CharField(max_length=255, blank=True, null=True)
    locality = models.CharField(max_length=255, blank=True, null=True)
    country = models.CharField(max_length=255, blank=True, null=True)
    administrative_area_level_5 = models.CharField(max_length=255, blank=True, null=True)
    administrative_area_level_4 = models.CharField(max_length=255, blank=True, null=True)
    administrative_area_level_3 = models.CharField(max_length=255, blank=True, null=True)
    administrative_area_level_2 = models.CharField(max_length=255, blank=True, null=True)
    administrative_area_level_1 = models.CharField(max_length=255, blank=True, null=True)
    postal_code = models.CharField(max_length=45, blank=True, null=True)
    route = models.CharField(max_length=255, blank=True, null=True)
    street_number = models.CharField(max_length=45, blank=True, null=True)
    phone = models.CharField(max_length=255, blank=True, null=True)
    phone2 = models.CharField(max_length=255, blank=True, null=True)
    phone3 = models.CharField(max_length=255, blank=True, null=True)
    email = models.CharField(max_length=255, blank=True, null=True)
    email2 = models.CharField(max_length=255, blank=True, null=True)
    email3 = models.CharField(max_length=255, blank=True, null=True)
    website = models.CharField(max_length=255, blank=True, null=True)
    facebook = models.CharField(max_length=255, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'business_information'

class UserBusinessInformation(models.Model):
    user = models.ForeignKey(AuthUser, models.DO_NOTHING)
    business = models.ForeignKey(BusinessInformation, models.DO_NOTHING)

    class Meta:
        managed = False
        db_table = 'user_business_information'

当我尝试在我的视图中访问UserBusinessInformation时,我既不使用_set也不管理。

def school(request, schoolname):
    school_searched = BusinessInformation.objects.get(name=schoolname)
    user_linked = school_searched.userbusinessinformation_set.all()

2 个答案:

答案 0 :(得分:1)

我想念多对多的领域:

class BusinessInformation(models.Model):
    business_id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255)
    users = models.ManyToManyField(AuthUser, 
                                   through='UserBusinessInformation')
    ...

然后,在您看来:

def school(request, schoolname):
    school_searched = BusinessInformation.objects.get(name=schoolname)
    user_linked = school_searched.users.all()

引用Extra fields on many-to-many relationships django文档:

  

对于这些情况,Django允许您指定将用于管理多对多关系的模型。然后,您可以在中间模型上添加额外的字段。中间模型与ManyToManyField相关联,使用through参数指向将充当中介的模型。

让我完成一些建议,这是真的,'这些是我的应用程序的相关类,但你可以用几个字段来说明这个示例。了解How to create a Minimal, Complete, and Verifiable example

答案 1 :(得分:1)

[if you want access a field that’s a ForeignKey, you’ll get the related model object just like]

    from django.db import models

       class Publisher(models.Model):
           name = models.CharField(max_length=30)
           address = models.CharField(max_length=50)
           city = models.CharField(max_length=60)
           state_province = models.CharField(max_length=30)
           country = models.CharField(max_length=50)
           website = models.URLField()`enter code here`
           def __str__(self):
              return self.name

        class Author(models.Model):
          first_name = models.CharField(max_length=30)
          last_name = models.CharField(max_length=40)
          email = models.EmailField()

          def __str__(self):
              return '%s %s' % (self.first_name, self.last_name)

        class Book(models.Model):
            title = models.CharField(max_length=100)
            authors = models.ManyToManyField(Author)
            publisher = models.ForeignKey(Publisher)
            publication_date = models.DateField()

        def __str__(self):
            return self.title 

    [you can access like that.... ]

    b = Book.objects.get(id=50)
    b.publisher
    b.publisher.website