在django模板中访问子模型属性

时间:2017-08-01 08:43:49

标签: python django python-3.x django-models

我在django模型中创建了模型继承,如:

models.py

class Instance(models.Model):
  id = models.AutoField(primary_key=True)
  user = models.ForeignKey(User, related_name='instances')
  name = models.CharField(max_length=255, name='name')
  slug = AutoSlugField(populate_from='name', unique=True)
  created_at = models.DateTimeField(default=timezone.now, editable=False)
  type = models.CharField(max_length=255, name='type', default='')
  class Meta:
    ordering = ['-created_at']


class CustomInstance(Instance):
  serverCode = models.TextField(default='', blank=False)
  jsonPackageCode = models.TextField(default='', blank=False)
  type = models.CharField(max_length=255, name='type', default='custom')


class AwdInstance(Instance):
  archive = models.FileField(upload_to='archives', name='archive')
  type = models.CharField(max_length=255, name='type', default='awd')


class AwodInstance(Instance):
  archive = models.FileField(upload_to='archives', name='archive')
  type = models.CharField(max_length=255, name='type', default='awod')


class GithubInstance(Instance):
  archive = models.CharField(max_length=255)
  type = models.CharField(max_length=255, name='type', default='github')

如何在django模板中获取子模型字段,例如 user.instances.serverCode?

请帮帮我! 提前谢谢!

0 个答案:

没有答案