2

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) 
1
  • Error is in your test.py - can you post the body of send_suggestion_email. Commented Oct 16, 2015 at 22:55

1 Answer 1

5

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).

Sign up to request clarification or add additional context in comments.

2 Comments

that's it. I am used to get_or_create. Thanks, I better give myself a break and go to sleep.
I will add it to my answer in case someone does the same thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.