1

如何创建从我的 model.py 文件调用多个类/模型的列表视图?

我已经创建了我的项目、应用程序和模型。这些模型使用一对多的关系。

我能够基于调用单个模型创建一个列表视图,但不能使用多个模型(在本场景中为 7 个模型)。

这是我要调用的模型,我希望在列表视图中显示的所需模型值用 ** 表示:

class TypeAchievement(models.Model):
  typeAchievementID = models.AutoField(primary_key=True, db_column="TypeAchievementID")
  languageCodeID = models.ForeignKey(LanguageCode, db_column="LanguageCodeID")
  **typeAchievementDescriptionIntl = models.CharField(max_length=255, db_column="TypeAchievementDescriptionIntl")**
  typeAchievementDescriptionEng = models.CharField(max_length=255, db_column="TypeAchievementDescriptionEng")
  typeAchievementAltID = models.IntegerField(db_column="TypeAchievementAltID")
  class Meta:
    db_table="TypeAchievement"

class RIAchievement(models.Model):
  riAchievementID = models.AutoField(primary_key=True, db_column="RIAchievementID")
  userLanguageVersionID = models.ForeignKey(UserLanguageVersion, db_column="UserLanguageVersionID")
  typeAchievementID = models.ForeignKey(TypeAchievement, db_column="TypeAchievementID")
  **riAchievementTypeUserDescription = models.CharField(max_length=255, blank=True, null=True, db_column="RIAchievementTypeUserDescription")**
  **riAchievementDescription = models.TextField(max_length=2000, db_column="RIAchievementDescription")**
  auth_user_id = models.ForeignKey(auth_user, db_column="auth_user_id")
  class Meta:
    db_table="RIAchievement"

class UserLanguageVersion(models.Model):
  userLanguageVersionID = models.AutoField(primary_key=True, db_column="UserLanguageVersionID")
  auth_user_id = models.ForeignKey(auth_user, db_column="auth_user_id")
  languageCodeID = models.ForeignKey(LanguageCode, db_column="LanguageCodeID")
  class Meta:
    db_table="UserLanguageVersion"

class auth_user(models.Model):
  **auth_user_id = models.AutoField(primary_key=True, db_column="id")**
  username = models.CharField(max_length=30, unique=True, db_column="username")
  first_name = models.CharField(max_length=30, db_column="first_name")
  last_name = models.CharField(max_length=30, db_column="last_name")
  email = models.CharField(max_length=75, db_column="email")
  password = models.CharField(max_length=128, db_column="password")
  is_staff = models.BooleanField(db_column="is_staff")
  is_active = models.BooleanField(db_column="is_active")
  is_supervisor = models.BooleanField(db_column="is_supervisor")
  last_login = models.DateTimeField(db_column="last_login")
  date_joined = models.DateTimeField(db_column="date_joined")
  class Meta:
    db_table="auth_user"

class LanguageCode(models.Model):
  languagecodeID = models.AutoField(primary_key=True, db_column="LanguageCodeID")
  **languageCodeDescription = models.CharField(max_length=10, db_column="LanguageCodeDescription")**
  baseLanguageCode = models.CharField(max_length=10, db_column="BaseLanguageCode")
  languageNameEng = models.CharField(max_length=255, db_column="LanguageNameEng")
  altLanguageNameEng = models.CharField(max_length=255, blank=True, null=True, db_column="AltLanguageNameEng")
  **languageNameNative = models.CharField(max_length=255, db_column="LanguageNameNative")**
  altLanguageNameNative = models.CharField(max_length=255, blank=True, null=True, db_column="AltLanguageNameNative")
  **iso639_1 = models.CharField(max_length=10, blank=True, null=True, db_column="ISO639_1")**
  iso639_2T = models.CharField(max_length=10, db_column="ISO639_2T")
  iso639_2B = models.CharField(max_length=10, db_column="ISO639_2B")
  iso639_X = models.CharField(max_length=10, db_column="ISO639_X")
  languageDirectionID = models.ForeignKey(LanguageDirection, default=1, db_column="LanguageDirectionID")
  class Meta:
    db_table="LanguageCode"

class LanguageDirection(models.Model):
  languageDirectionID = models.AutoField(primary_key=True, db_column="LanguageDirectionID")
  **languageDirectionDescription = models.CharField(max_length=20, db_column="LanguageDirectionDescription")**
  **languageDirDescription = models.CharField(max_length=20, db_column="LanguageDirDescription")**
  **textAlign = models.CharField(max_length=20, db_column="TextAlign")**
  **oppositeLanguageDirectionDescription = models.CharField(max_length=20, db_column="OppositeLanguageDirectionDescription")**
  **oppositeLanguageDirDescription = models.CharField(max_length=20, db_column="OppositeLanguageDirDescription")**
  **oppositeTextAlign = models.CharField(max_length=20, db_column="OppositeTextAlign")**
  class Meta:
    db_table="LanguageDirection"

class Flag(models.Model):
  flagID = models.AutoField(primary_key=True, db_column="FlagID")
  languageCodeID = models.ForeignKey(LanguageCode, db_column="LanguageCodeID")
  **flagIconPath = models.CharField(max_length=255, db_column="FlagIconPath")**
  flagDescription = models.CharField(max_length=255, db_column="FlagDescription")
  flagInformation = models.CharField(max_length=255, db_column="FlagInformation")
  class Meta:
    db_table="Flag"
4

1 回答 1

3

您可以从主对象获取链接对象。在您的情况下 - 遍历列表RIAchievement并获取其他数据{{ riachievement.userLanguageVersionID.languageCodeID.languageDirectionID.textAlign }}

PS 另外,请根据 pep8格式化代码,即使它是从数据库自动生成的。

于 2012-04-19T05:02:13.407 回答