Lucas03
Lucas03

Reputation: 2345

TypeError: object is not iterable when creating object

probably it's too late because I totaly do not understand this error. I created two new classes in 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')

And then I have a code:

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)

And this is error traceback: Traceback (most recent call last):

  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

What does this mean? Error fires up when ObjectDoesNotExist and I want to create new object SuggestionEmailContent. user.suggestion_set is of type <class 'suggestion.models.SuggestionEmailSent'> as it should be. What am I missing? I am using django 1.8


Edit1:
Here is my test.py:

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

and this is my 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) 

Upvotes: 2

Views: 3349

Answers (1)

Ivan
Ivan

Reputation: 6013

create only returns the created instance instead of (instance, created), so your assignment tries to unpack it.

get_or_create on the other hand does return (instance, created).

Upvotes: 5

Related Questions