Retrieving reddit posts with Django

398 views Asked by At

As a way of learning Python/Django, I'm trying to recreate reddit in Django using PRAW, but I'm running into a few issues. Currently, I have my app configured to connect to reddit and retrieve the top 10 posts from the home page in my IndexView class, but this is causing problems when I try to add fields in my Posts model then use makemigrations. Even when I delete everything out of my database from django admin, if I add a field to my model, then use python manage.py makemigrations, I frequently get an error that says no such column: post_is_self or something similar. When I comment out the new field, reboot the django server, the posts are back in the database, even though I haven't accessed the index of my app in the meantime.

I believe that the problem is with the way I'm populating the database. I want to retrieve the latest top 10 posts when the user goes to the index of my app, but now adding new fields is incredibly difficult. It seems as if it's retrieving posts when it shouldn't, and this is causing problems when a new field is added to the model and makemigrations is being called.

Here is my IndexView class:

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]

Edit: ...and my Post model:

class Post(models.Model):
    def __unicode__(self):
        return self.post_title
    id = models.CharField(
        verbose_name='Post ID',
        primary_key=True,
        max_length=200
    )
    post_title = models.CharField(
        verbose_name='Post title',
        max_length=200
    )
    post_submitted_on = models.DateTimeField(
        verbose_name='Submitted on',
        null=True
    )
    post_upvotes = models.IntegerField(
        verbose_name='Upvotes',
        null=True
    )
    post_downvotes = models.IntegerField(
        verbose_name='Downvotes',
        null=True
    )
    post_score = models.IntegerField(
        verbose_name='Score',
        null=True
    )
    post_submitter = models.CharField(
        verbose_name='Submitted by',
        null=True,
        max_length=15
    )
    post_subreddit_id = models.CharField(
        verbose_name='Subreddit ID',
        null=True,
        max_length=200
    )
    post_comment_count = models.IntegerField(
        verbose_name='Number of comments', 
        null=True
    )
    post_permalink = models.URLField(
        verbose_name='Permalink',
        null=True
    )
    post_url = models.URLField(
        verbose_name='Post URL',
        null=True
    )
    post_subreddit = models.CharField(
        verbose_name='Subreddit',
        null=True,
        max_length=200
    )
    post_thumbnail = models.URLField(
        verbose_name='Thumbnail URL',
        null=True
    )
    #post_is_self = models.BooleanField(
    #   verbose_name='Is self post?',
    #   null=True,
    #   default=False
    #)
    post_selftext = models.TextField(
        verbose_name='Self post text',
        max_length=40000,
        default='Not a self post'
    )

Should I be retrieving the top posts somewhere else? Is it retrieving the posts before the new field is added to the model? Any help is appreciated, thanks!

0

There are 0 answers