TypeError:对象在创建对象时不可迭代

时间:2015-10-16 22:47:08

标签: django

可能为时已晚,因为我完全不理解这个错误。我在models.py中创建了两个新类:

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) 

1 个答案:

答案 0 :(得分:4)

create仅返回已创建的实例而不是(instance, created),因此您的作业会尝试将其解压缩。

另一方面,

get_or_create会返回(instance, created)