1

model.py ファイルから複数のクラス/モデルを呼び出すリスト ビューを作成するにはどうすればよいですか?

プロジェクト、アプリ、モデルを作成しました。モデルは 1 対多の関係を使用します。

単一のモデルの呼び出しに基づいてリストビューを作成できますが、複数のモデル (このシナリオでは 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 に答える