2014-07-22 96 views
-1

我試圖運行它拋出AttributeError的: '名單' 對象有沒有屬性 '對象'

AttributeError: 'list' object has no attribute 'objects'

script.py

#Get Dota2 Item Rarities 
dotaItemRarityUrl = 'http://api.steampowered.com/IEconDOTA2_570/GetRarities/v1?' 
dotaItemRarityPayload = {'key': settings.SOCIAL_AUTH_STEAM_API_KEY, 
        'language': 'en', 
        } 
dotaItemRarityInfo = requests.get(dotaItemRarityUrl, params=dotaItemRarityPayload) 
dotaItemRarity = dotaItemRarityInfo.json() 
dotaItemRarity = dotaItemRarity['result']['rarities'] 
print(dotaItemRarity) 
#print(dotaItemQualities) 

#Populate Database With Item Rarities that do NOT exist already 
for rarity in dotaItemRarity: 
    print rarity 
    irarityId = rarity['id'] 
    irarityProperName = rarity['localized_name'] 
    irarityInternalName = rarity['name'] 
    irarityColor = rarity['color'] 

    q = dotaItemRarity.objects.filter(rarityId=irarityId) 
    print q 

    if len(q) == 0: 
     newRarity = dotaItemRarity(rarityId=irarityId, 
            rarityProperName=irarityProperName, 
            rarityInternalName=irarityInternalName, 
            rarityColor=irarityColor) 
     newRarity.save() 

models.py以下

class dotaItemRarity(models.Model): 
    rarityId = models.IntegerField(max_length=3,primary_key=True) 
    rarityProperName = models.CharField(max_length=60) 
    rarityInternalName = models.CharField(max_length=50) 
    rarityColor = models.CharField(max_length=30) 

    def __unicode__(self): 
     return self.rarityInternalName 

我正在使用sout h來處理遷移,並嘗試了多種方法來解決這個問題。刪除表格並重建它們。據我所知,這應該工作,任何人都可以指向正確的方向。

+0

你定義一個名爲列表'dotaItemRarity',它會以相同的名稱遮蓋您導入的模型。重命名你的清單。 – Blender

回答

1

dotaItemRarity是一個列表,它沒有objects屬性:

q = dotaItemRarity.objects.filter(rarityId=irarityId) 

那是因爲你從你的JSON結果綁定了一個清單:

dotaItemRarity = dotaItemRarityInfo.json() 
dotaItemRarity = dotaItemRarity['result']['rarities'] 

這是一個Django模型,就像你期望的那樣。

如果您將dotaItemRarity Django模型導入到script.py中,則名稱不再綁定到該模型,因爲您已將其替換爲該列表。

將列表重命名爲使用不掩蓋模型的其他名稱。

請注意,Python style guide建議您爲類(包括Django模型)使用CamelCase名稱,以避免此類錯誤。

繼PEP 8重構你的代碼一點,以及使用一些更清晰命名的做法:

models.py

class DotaItemRarity(models.Model): 
    rarity_id = models.IntegerField(max_length=3, primary_key=True) 
    rarity_proper_name = models.CharField(max_length=60) 
    rarity_internal_name = models.CharField(max_length=50) 
    rarity_color = models.CharField(max_length=30) 

    def __unicode__(self): 
     return self.rarity_internal_name 

script.py

#Get Dota2 Item Rarities 
url = 'http://api.steampowered.com/IEconDOTA2_570/GetRarities/v1' 
payload = {'key': settings.SOCIAL_AUTH_STEAM_API_KEY, 'language': 'en'} 
response = requests.get(url, params=payload) 
rarities = response.json()['result']['rarities'] 

for rarity in rarities: 
    rarity_id = rarity['id'] 

    try: 
     DotaItemRarity.get(rarity_id=rarity_id) 
    except DotaItemRarity.DoesNotExist: 
     new_rarity = DotaItemRarity(
      rarityId=rarity_id, 
      rarity_proper_name=rarity['localized_name'], 
      rarity_internal_name=rarity['name'], 
      rarity_color=rarity['color']) 
     new_rarity.save() 
+0

哇......非常感謝。我的這個愚蠢的錯誤,但感謝指導我在最佳實踐方面的正確方向。這應該有助於避免這種情況再次發生。 – Deep

相關問題