我们如何从子域模型获得组织

时间:2014-04-03 11:07:55

标签: django python-2.7 django-models

class Subdomain(models.Model):
    subdomain = models.SlugField(max_length=50)
    organisation = models.OneToOneField('Organisation', related_name='subdomain')

class Organisation(CommonInfo):
        ORGANISATION_TYPE = (('C', 'Company'),
                             ('P', 'Product provider'),
                             ('D', 'Demo Company'))   
type_of_organisation = models.CharField(choices=ORGANISATION_TYPE, max_length=1) #this field cannot be null
        name = models.CharField(max_length=50)
        organisation_details = models.OneToOneField('OrganisationDetails', null=True, blank=True, related_name='organisation')
        demo_start = models.DateTimeField(null=True, default=None, blank=True)
        demo_end = models.DateTimeField(null=True, default=None, blank=True)
        plan = models.ForeignKey(Plan, blank=True, null=True)
        description = models.TextField(blank=True, default='')

我想从Subdomain model.how获取组织从子域模型???

获取组织

1 个答案:

答案 0 :(得分:0)

models.py

class Subdomain(models.Model):
    subdomain = models.SlugField(max_length=50)
    organisation = models.OneToOneField('Organisation', related_name='subdomain')

class Organisation(CommonInfo):
    ORGANISATION_TYPE = (('C', 'Company'),
                         ('P', 'Product provider'),
                         ('D', 'Demo Company'))   
    type_of_organisation = models.CharField(choices=ORGANISATION_TYPE, max_length=1) #this field cannot be null
    name = models.CharField(max_length=50)
    organisation_details = models.OneToOneField('OrganisationDetails', null=True, blank=True, related_name='organisation')
    demo_start = models.DateTimeField(null=True, default=None, blank=True)
    demo_end = models.DateTimeField(null=True, default=None, blank=True)
    plan = models.ForeignKey(Plan, blank=True, null=True)
    description = models.TextField(blank=True, default='')

views.py

subdomain = Subdomain.objects.get(subdomain='news')
subdomain.organisation

看起来太容易了:D