1
votes

What I did:

  1. I deleted the database
  2. deleted the pycache files and the migrations files
  3. python manage.py makemigrations app_name and python manage.py migrate app_name
  4. close the server several times

But this is the error I received:

enter image description here

settings.py

INSTALLED_APPS = [
    'blog',
    'users',
    'crispy_forms',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

]

users/models.py

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    image = models.ImageField(default='default.jpg', upload_to='profile_pics')

    def __str__(self):
        return f'{self.user.username} Profile'

    def save(self, *args, **kwargs):
        super().save()

        img = Image.open(self.image.path)

        if img.height > 300 or img.width > 300:
            output_size = (300, 300)
            img.thumbnail(output_size)
            img.save(self.image.path)

blog/models.py

class Post(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    date_posted = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('post-detail', kwargs={'pk': self.pk})
3
if you specifically mention your app name in migration, you have to do it for both of your apps, otherwise don't mention app name, so the migration will be applied to all the apps if any changes happened to models - bmons

3 Answers

2
votes

I think you havn't migrated your blog app models properly. can you show your migration file of blog app And you should define your db_name in class Meta in your models otherwise your models will attatched to your app and this can trouble you in future

1
votes

Migrate your blog app using this command

python manage.py makemigrations blog
0
votes

Please run

     1. python manage.py makemigrations
     2. python manage.py migrate
     3. python manage.py runserver