2015-10-16 55 views
1

可能已經太晚了,因爲我總是不明白這個錯誤。我在models.py創建了兩個新類:TypeError:創建對象時不能迭代對象

class SuggestionEmailSent(models.Model): 
    user = models.OneToOneField(User, related_name='suggestion_sent') 
    frequency = models.CharField(max_length=10, choices=EMAIL_FREQUENCY_CHOICES, default=EMAIL_FREQUENCY_CHOICES[0][0]) 
    date = models.DateField(default=timezone.now) 
    class Meta: 
     unique_together = ("user", "date")  

class SuggestionEmailContent(models.Model): 
    percentage = models.IntegerField() 
    buy_stock = models.ForeignKey('stocks.Stock', related_name='suggestion_email_buy_stock') 
    sell_stock = models.ForeignKey('stocks.Stock', related_name='suggestion_email_sell_stock') 
    portfolio = models.OneToOneField('portfolio.Portfolio', unique=True) 
    suggestion_sent = models.ForeignKey(SuggestionEmailSent, related_name='contents') 

然後,我有一個代碼:

try: 
    content = user.suggestion_sent.contents.get(portfolio=portfolio) 
    print content.sell_stock 
except ObjectDoesNotExist: #mail not sent for this portfolio, send and save 
    content, created = SuggestionEmailContent.objects.create(percentage=percentage, 
     buy_stock=suggestion, 
     sell_stock=rank, 
     portfolio=portfolio, 
     suggestion_sent=user.suggestion_sent) 

這是錯誤回溯: 回溯(最近通話最後一個):

File "./test.py", line 49, in <module> 
    send_suggestion_email(User.objects.get(id=1)) 
    File "/var/www/django/digrin/wsgi/digrin/suggestion/utils.py", line 192, in send_suggestion_email 
    suggestion_sent=user.suggestion_sent) 
TypeError: 'SuggestionEmailContent' object is not iterable 

這是什麼意思?當ObjectDoesNotExist和我想要創建新對象SuggestionEmailContent時,錯誤啓動。 user.suggestion_set類型爲<class 'suggestion.models.SuggestionEmailSent'>,因爲它應該是。我錯過了什麼?我使用Django 1.8


EDIT1:
這裏是我的test.py:

if __name__ == '__main__': 
    from suggestion.utils import * 
    send_suggestion_email(User.objects.get(id=1)) 

,這是我send_suggestion_email:

def send_suggestion_email(user): 
    percentage = 100 
    for portfolio in Portfolio.objects.filter(testing=False, user=user): 
     dividends, monthly_shares = get_portfolio_month_shares(portfolio) 
     shares_price = get_portfolio_current_year_price(monthly_shares) 
     suggestions, ranks = get_suggestion_data(portfolio=portfolio, shares=monthly_shares) 
     if not suggestions or not ranks: 
      print "no suggestions nor ranks for portfolio" + str(portfolio.id) 
      continue 
     suggestion, rank = suggestions.keys()[0], ranks.keys()[0] 
     try: 
      content = user.suggestion_sent.contents.get(portfolio=portfolio) 
      print content.sell_stock 
     except ObjectDoesNotExist: #mail not sent for this portfolio, send and save 
      content, created = SuggestionEmailContent.objects.create(percentage=percentage, 
       buy_stock=suggestion, 
       sell_stock=rank, 
       portfolio=portfolio, 
       suggestion_sent=user.suggestion_sent) 
+0

錯誤在你的'test.py'中 - 你可以發佈'send_suggestion_email'的主體。 –

+0

檢查編輯1請 – Lucas03

回答

3

create只返回所創建的而不是(instance, created),所以你的任務試圖解開它。

get_or_create另一方面確實返回(instance, created)

+1

就是這樣。我習慣於'get_or_create'。謝謝,我最好讓自己休息一下,然後去睡覺。 – Lucas03

+0

我會將其添加到我的答案,以防有人做同樣的事情。 – Ivan

相關問題