作为学习Python / Django的一种方式,我尝试使用PRAW在Django中重新创建reddit,但我遇到了一些问题。目前,我已将我的应用程序配置为连接到reddit并从我的IndexView类中的主页检索前10个帖子,但是当我尝试在Posts
模型中添加字段然后使用{{1 }}。即使我从django admin中删除了我的数据库中的所有内容,如果我向模型中添加一个字段,然后使用makemigrations
,我经常会收到一条错误,上面写着python manage.py makemigrations
或类似内容。当我注释掉新字段时,重新启动django服务器,帖子又回到了数据库中,即使我还没有同时访问我的应用程序的索引。
我认为问题在于我填充数据库的方式。我想在用户访问我的应用程序索引时检索最新的前10个帖子,但现在添加新字段非常困难。似乎它不应该检索帖子,这会在向模型添加新字段并调用makemigrations时导致问题。
这是我的IndexView类:
no such column: post_is_self
编辑:...和我的class IndexView(generic.ListView):
template_name = 'reddit/index.html'
context_object_name = 'top_posts'
#Get top 10 posts from the front page
pp = pprint.PrettyPrinter(indent = 4)
r = praw.Reddit(user_agent = user_agent)
front_page = r.get_front_page(limit = thing_limit)
for thing in front_page:
# Format timestamp
utc_dt = datetime.utcfromtimestamp(thing.created_utc).replace(
tzinfo=pytz.utc
)
local_dt = local_tz.normalize(utc_dt.astimezone(local_tz))
post = Post(
id = thing.id,
post_title = thing.title,
post_submitted_on = local_dt,
post_upvotes = thing.ups,
post_downvotes = thing.downs,
post_score = thing.score,
post_submitter = thing.author,
post_comment_count = thing.num_comments,
post_permalink = thing.permalink,
post_url = thing.url,
post_subreddit = thing.subreddit,
post_subreddit_id = thing.subreddit_id,
post_thumbnail = thing.thumbnail,
)
if thing.is_self:
post.post_selftext = thing.selftext
post.save()
def get_queryset(self):
# Return top 10 stories, in descending order by score
return Post.objects.all().order_by('-post_score')[:10]
模型:
Post
我应该在其他地方检索热门帖子吗?是否在将新字段添加到模型之前检索帖子?感谢任何帮助,谢谢!