Can not delete superuser in django and a resulting error django.db.utils.OperationalError: no such column: infrastructure_profile.slug

百般思念 提交于 2020-01-16 09:05:37

问题


I wanted to add a slug field to my model Profile (that extends the User model) after I had several profiles created, then an error appeared when reaching the profile page with the slug in the url saying :

Django OperationalError: no such column: infrastructure_profile.slug

so I looked here and saw this answer

and it suggested I delete all my migrations files to restart the database , so I did

and then I got the same error , so I thought I should delete all the users I already have that didn't had the slug field already including the superuser.

so I followed this answer

and I got that error

django.db.utils.OperationalError: no such column: infrastructure_profile.slug

any idea what's going on ?

Edit

My models.py contains the model Profile like so

class Profile(User):
    user = models.OneToOneField(User, parent_link=True, on_delete=models.CASCADE)
    bio = models.TextField()
    slug = models.SlugField(unique=True, blank=True)
    avatar_thumbnail = ProcessedImageField(upload_to='images/',
                                            default='/images/default.png',
                                           processors=[ResizeToFill(300, 300)],
                                           format='JPEG',
                                           options={'quality': 60})
    location = models.TextField()
    tags = models.ManyToManyField(Tag)
    contact_information = models.TextField()
    verified = models.BooleanField(default=False)
    counter = models.IntegerField(default=0)

    def __str__(self):
        return self.user.username

    def save(self, *args, **kwargs):
        print('self.username')
        print(self.user.username)
        self.slug = self.user.username
        super(Profile, self).save(*args, **kwargs)

    def get_absolute_url(self):
        return reverse('infrastructure:edit-user-profile', kwargs={'slug': self.slug})


回答1:


Delete the db and delete all the files in the migration folder except__init__.py file in all your apps will work. After than do migration again




回答2:


I shouldn't have inherited Profile from the User model and I should have customized the user to following this answer instead

https://stackoverflow.com/a/58904426/5752406



来源:https://stackoverflow.com/questions/58885867/can-not-delete-superuser-in-django-and-a-resulting-error-django-db-utils-operati

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!