2012-05-13 122 views
1

我試圖使用Django prefetch_related,因爲下面顯示的模型LanguageCodeFlag(我試圖顯示flagIconPath)之間的一對多關係,但我似乎無法得到views.py代碼正確,我真的需要一些幫助!如何讓Django prefetch_related工作?

這裏是我的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}) 

什麼代碼我應該使用,以取代???的?

這裏是我試圖讓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" 

回答

3

Aagh,我的眼睛。請給你的領域適當Pythonic名稱。特別是因爲您已經在使用db_column屬性,所以您可以保持數據庫原樣並給出我們實際可以讀取的字段名稱。

特別是,您的ForeignKey字段不應該被稱爲fooId:基礎數據庫字段是一個ID,但該字段不是,它是對實際FK模型的引用。

要回答你的問題,你提到的所有FK都是前向引用,而不是倒退。所以你需要在這裏使用select_related,而不是prefetch_related

+0

恩,請原諒我,如果我錯了,我是一個neewbie @ django&python,但模型語言代碼和標誌之間的關係是1到很多,所以我不能使用select_related(我嘗試過並失敗了)。 – user1261774

+0

使用** select_related **這不返回flagIconPath,bc是1對多關係: qs = RIAchievement.objects.select_related(「riachievement」,「typeachievement」,「userlanguageversion」,「languagecode」,「languagedetail」, 「languagedirection」,「flag」)。all() – user1261774

+0

如何使用** prefetch_related **返回flagIconPath? – user1261774