0

I have trouble writing a blog application in Django.

First, my models.py:

class Blog(models.Model):
    user = models.ForeignKey(User)
    blog_title = models.TextField()
    def __unicode__(self):
        return self.blog_title

class PostManager(models.Manager):
    def create_post(self, blog):
        post = self.create(blog=blog)
        return post

class Post(models.Model):
    blog = models.ForeignKey(Blog)
    post_title = models.TextField()
    post_content = models.TextField()
    objects = PostManager()
    def __unicode__(self):
        return self.post_title

And then the lines in the views.py file that causes error:

user = request.user
blog = Blog.objects.get_or_create(
    user=user
)
post = Post.objects.create_post(blog)

This line causes the error:

Cannot assign "(<Blog: title1>, False)": "Post.blog" must be a "Blog" instance.

Request Method:     POST
Request URL:    http://localhost:7777/save/
Django Version:     1.5.1
Exception Type:     ValueError
Exception Value:    

Cannot assign "(<Blog: title1>, False)": "Post.blog" must be a "Blog" instance.

Exception Location:     /Library/Python/2.7/site-packages/django/db/models/fields/related.py in __set__, line 405
Python Executable:  /usr/bin/python
Python Version:     2.7.2
Python Path:    

['/Users/yoongeemin/Documents/Django/project_blog',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
 '/Library/Python/2.7/site-packages']

I've been trying to fix this for hours, yet I have no clue what the error means and what's causing the problem. Any suggestions :( ?? Thanks!

user2492270
  • 2,215
  • 6
  • 40
  • 56
  • @soon Well, it is the 3rd line ("post = Post.objects.create(blog)") that causes the error – user2492270 Jun 17 '13 at 06:22
  • 4
    Please, read the answer for the question I linked above. `get_or_create` returns tuple with two elements. You have to change `blog = Blog.objects.get_or_create(user=user)` to `blog, created = Blog.objects.get_or_create(user=user)`. – awesoon Jun 17 '13 at 06:24
  • @soon WOOOOW I can't believe I missed that lol.. I've been trying to fix the wrong line for hours.... I feel stupid now.. Thank you so much!! – user2492270 Jun 17 '13 at 06:26
  • 1
    I have found this question less than minute by query `django objects get or create` in the Google :) Glad it helped, anyway. – awesoon Jun 17 '13 at 06:29

0 Answers0