Search code examples
djangogitdjango-modelspythonanywhere

Issue in making changes on the site, hosted on pythonanywhere


I'm hosting my blog site on pythonanywhere. I also have a model field for subscribers in the database. The problem is whenever ever I create a new post locally and pull it in pythonanywhere bash console. The local database replaces the production database. Which results in losing all the data provided by the users. How to stop some fields from changing on every pull request?

class Category(models.Model):
    created_on = models.DateTimeField(auto_now_add=True, verbose_name="Created on")
    updated_on = models.DateTimeField(auto_now=True, verbose_name="Updated on")
    title = models.CharField(max_length=255, verbose_name="Title")

    class Meta:
        verbose_name = "Category"
        verbose_name_plural = "Categories"
        ordering = ['title']

    def __str__(self):
        return self.title

class Post(models.Model):
    author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    title = models.CharField(max_length=200)
    text = models.TextField()
    image_file = models.ImageField(upload_to='media', null=True, blank=True)
    image_url = models.URLField(null=True)
    category = models.ForeignKey(Category, verbose_name="Category", on_delete=models.CASCADE, null=True)
    published_date = models.DateTimeField(blank=True, default=timezone.now ,null=True)

    class Meta:
        verbose_name = "Post"
        verbose_name_plural = "Posts"
        ordering = ('-published_date',)

    def get_absolute_url(self):
        return reverse("post_detail",kwargs={'pk':self.pk})

    def __str__(self):
        return self.title


class Comment(models.Model):
    post = models.ForeignKey(Post,on_delete=models.CASCADE,related_name='comments')
    name = models.CharField(max_length=80)
    body = models.TextField()
    created_on = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ('-created_on',)

    def __str__(self):
        return 'Comment {} by {}'.format(self.body, self.name)


class Subscribe(models.Model):
    email = models.EmailField()
    subscribed_on = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ('-subscribed_on',)

    def __str__(self):
        return 'Subscribed by {} on {}'.format(self.email, self.subscribed_on)

Solution

  • It sounds like you're using sqlite and that you have included the database file in git. That means that every time you commit (either on PythonAnywhere or locally) you are replacing your database with the one that you commited. You can exclude your database from the repo by adding its filename to your .gitignore file and removing it from the repo (with git rm --staged).