2015-10-16 43 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