1

prefetch_relatedモデル間の 1 対多の関係のためにDjango を使用しようとしていLanguageCodeますFlagが (私は を表示しようとしていますflagIconPath)、コードを正しく取得できないようで、views.py本当に助けが必要です!

ここに私のviews.pyフォームがあります:

from django.shortcuts import render

from app_data.models import RIAchievement

def ri_achievements(request):

  qs = RIAchievement.objects.all().prefetch_related("??????", "????????")

  return render(request, 'index.html',{'qs': qs})

???'s? を置き換えるには、どのコードを使用する必要がありますか?

django prefetch_related を表示しようとしているフィールドは次のとおりです。

(flag) flagiconpath,
(riachievement) riAchievementTypeUserDescription,
(typeachievement) typeAchievementDescriptionIntl,
(languagecode) languageNameNative,
(flag) flagIconPath,
(languagedetail) languageDetailDescription,
(languagedirection) languageDirectionDescription,
(riusersettings) riUserSettingsDisplayMultipleLanguage,
(typeresumestyle) typeResumeStyleNumber,
(auth_user) id.

関連するモデルは次のとおりです。

class RIAchievement(models.Model):
  riAchievementID = models.AutoField(primary_key=True, db_column="riAchievementID")
  userLanguageVersionID = models.ForeignKey(UserLanguageVersion, db_column="userLanguageVersionID", related_name="riAchievement_userLanguageVersionID")
  typeAchievementID = models.ForeignKey(TypeAchievement, db_column="typeAchievementID", related_name="riAchievement_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", related_name="riAchievement_auth_user_id")
  class Meta:
    db_table="riAchievement"

class TypeAchievement(models.Model):
  typeAchievementID = models.AutoField(primary_key=True, db_column="typeAchievementID")
  languageCodeID = models.ForeignKey(LanguageCode, db_column="languageCodeID", related_name="typeAchievement_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 UserLanguageVersion(models.Model):
  userLanguageVersionID = models.AutoField(primary_key=True, db_column="userLanguageVersionID")
  auth_user_id = models.ForeignKey(auth_user, db_column="auth_user_id", related_name="userLanguageVersion_auth_user_id")
  languageCodeID = models.ForeignKey(LanguageCode, db_column="languageCodeID", related_name="userLanguageVersion_languageCodeID")
  class Meta:
    db_table="userLanguageVersion"

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", related_name="languageCode_languageDirectionID")
  class Meta:
    db_table="languageCode"

class Flag(models.Model):
  flagID = models.AutoField(primary_key=True, db_column="flagID")
  languageCodeID = models.ForeignKey(LanguageCode, db_column="languageCodeID", related_name="flag_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"

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 RIUserSettings(models.Model):
  riUserSettingsID = models.AutoField(primary_key=True, unique=True, db_column="riUserSettingsID")
  riUserSettingsPrimaryLanguageCodeID = models.ForeignKey(LanguageCode, db_column="riUserSettingsPrimaryLanguageCodeID", related_name="riUserSettings_riUserSettingsPrimaryLanguageCodeID")
  riUserSettingsDisplayPrimaryLanguage = models.BooleanField(default=False, db_column="riUserSettingsDisplayPrimaryLanguage")
  riUserSettingsDisplayMutipleLanguage = models.BooleanField(default=False, db_column="riUserSettingsDisplayMutipleLanguage")
  riUserSettingsDisplaySingleLanguage = models.BooleanField(default=False, db_column="riUserSettingsDisplaySingleLanguage")
  riUserSettingsPrimaryVirtualKeyboardLanguageVersionID = models.ForeignKey(UserLanguageVersion, db_column="riUserSettingsPrimaryVirtualKeyboardLanguageVersionID", related_name="riUserSettings_riUserSettingsPrimaryVirtualKeyboardLanguageVersionID")
  riUserSettingsDisplayPrimaryVirtualKeyboard = models.BooleanField(default=False, db_column="riUserSettingsDisplayPrimaryVirtualKeyboard")
  riUserSettingsDisplayRIFormattingPreview = models.BooleanField(default=True, db_column="riUserSettingsDisplayRIFormattingPreview")
  typeResumeStyleID = models.ForeignKey(TypeResumeStyle, db_column="typeResumeStyleID", related_name="riUserSettings_typeResumeStyleID")
  auth_user_id = models.ForeignKey(auth_user, db_column="auth_user_id", related_name="riUserSettings_auth_user_id")
  class Meta:
    db_table="riUserSettings"

class LanguageDetail(models.Model):
  languageDetailID = models.AutoField(primary_key=True, unique=True, db_column="languageDetailID")
  languageCodeID = models.ForeignKey(LanguageCode, db_column="languageCodeID", related_name="languageDetail_languageCodeID")
  nativeLanguageCodeID = models.ForeignKey(LanguageCode, db_column="nativeLanguageCodeID", related_name="languageDetail_nativeLanguageCodeID")
  languageDetailDescriptionIntl = models.CharField(max_length=255, db_column="languageDetailDescriptionIntl")
  languageDetailDescriptionEng = models.CharField(max_length=255, db_column="languageDetailDescriptionEng")
  languageDetailDescriptionNative = models.CharField(max_length=255, db_column="languageDetailDescriptionNative")
  displayCapitaliseCountryName = models.BooleanField(default=False, db_column="displayCapitaliseCountryName")
  languageDirectionID = models.ForeignKey(LanguageDirection, default=1, db_column="languageDirectionID", related_name="languageDetail_languageDirectionID")
  flagID = models.ForeignKey(Flag, db_column="flagID", related_name="languageDetail_FlagID")
  languageDetailAltID = models.IntegerField(db_column="languageDetailAltID")
  class Meta:
    db_table="languageDetail"

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 TypeResumeStyle(models.Model):
  typeResumeStyleID = models.AutoField(primary_key=True, db_column="typeResumeStyleID")
  languageCodeID = models.ForeignKey(LanguageCode, db_column="languageCodeID", related_name="typeResumeStyle_languageCodeID")
  typeResumeStyleNumber = models.IntegerField(db_column="typeResumeStyleNumber")
  typeResumeStyleNameIntl = models.CharField(max_length=255, db_column="typeResumeStyleNameIntl")
  typeResumeStyleNameEng = models.CharField(max_length=255, db_column="typeResumeStyleNameEng")
  typeResumeStyleDescriptionIntl = models.CharField(max_length=255, db_column="typeResumeStyleDescriptionIntl")
  typeResumeStyleDescriptionEng = models.CharField(max_length=255, db_column="typeResumeStyleDescriptionEng")
  class Meta:
    db_table="typeResumeStyle"
4

1 に答える 1

3

ああ、私の目。フィールドに適切な Pythonic の名前を付けてください。特に、すでにdb_column属性を使用しているため、DB をそのままにして、実際に読み取れるフィールド名を付けることができます。

特に、ForeignKey フィールドは fooId と呼ばれるべきではありません: 基礎となるデータベース フィールドは ID ですが、フィールドはそうではなく、実際の FK モデルへの参照です。

ただし、質問に答えるために、あなたが言及するすべての FK は後方参照ではなく前方参照です。select_relatedしたがって、ここでは ではなくを使用する必要がありprefetch_relatedます。

于 2012-05-14T08:13:39.977 に答える