私は2つのモデルを定義しています
from django.db import models
class Blog(models.Model):
title = models.CharField(max_length=144)
@property
def posts(self):
self.Post_set.all()
class Post(models.Model):
title = models.CharField(max_length=144)
text = models.TextField()
blog = models.ForeignKey('Blog')
しかし、問題は、シェルを実行して入力するときです
>>> blog = Blog(title="My blog")
>>> post = Post(title="My first post", text="Here is the main text for my blog post", blog=blog)
>>> blog.posts
エラーが発生します
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/lucas/Programming/Python/Django/djangorestfun/blog/models.py", line 9, in posts
self.Post_set.all()
AttributeError: 'Blog' object has no attribute 'Post_set'
>>>
今、私は次の問題を抱えています
>>> from blog.models import *
>>> blog = Blog(title="gewrhter")
>>> blog.save()
>>> blog.__dict__
{'_state': <django.db.models.base.ModelState object at 0x259be10>, 'id': 1, 'title': 'gewrhter'}
>>> blog._state.__dict__
{'adding': False, 'db': 'default'}
>>> post = Post(title="sdhxcvb", text="hdbfdgb", blog=blog)
>>> post.save()
>>> post.__dict__
{'blog_id': 1, 'title': 'sdhxcvb', 'text': 'hdbfdgb', '_blog_cache': <Blog: Blog object>, '_state': <django.db.models.base.ModelState object at 0x259bed0>, 'id': 1}
>>> blog.posts
>>> print blog.posts
None
2回目の更新
だから私はあなたのガイドに従いましたが、まだ何も得られません. さらに、blog.posts でエラーが発生します。
>>> from blog.models import *
>>> blog = Blog(title="asdf")
>>> blog.save()
>>> post = Post(title="asdf", text="sdxcvb", blog=blog)
>>> post.save()
>>> blog.posts
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'Blog' object has no attribute 'posts'
>>> print blog.all_posts
None