I created a custom user model just like the docs said, when I ran the makemigrations
I went to the folder of migrations and deleted the first name and last name columns inside the 0001_initial.py
and then I ran the migrate
command, but when I ran the createsuperuser
command it throwed an error in the terminal that django.db.utils.OperationalError: no such column: myapp_usermodel.first_name
How can I fix this? I want to delete the first name and last name columns because I'm not using them
EDIT: this is the user model
from django.db import models
from django.utils.translation import gettext_lazy as _
from django.contrib.auth.models import AbstractUser
# Create your models here.
class usermodel(AbstractUser):
email = models.EmailField(max_length=60)
username = models.CharField(max_length=20, unique=True)
password = models.CharField(max_length=20)
def __str__(self):
return self.username
For example, you may have to add additional fields to the user model or use email instead of a username for login. In fact, we have a couple of different ways to customize the Django user model. We mainly use these two methods. In this post, we'll use AbstractBaseUser to customize the user model.
Extend the Django user model with AbstractUser (preferred) # With AbstractUser, you can overwrite the standard User model. It will inherit all functions and current fields from the standard User class and you can add anything you would like to this. Here is an example that would be in models.py:
How to Create an App in Django? Inside the models.py add the following code: Register this model by adding the following code inside the admin.py. Some kinds of projects may have authentication requirements for which Django’s built-in User model is not always appropriate.
In this tutorial, we have the complete guide of the Django on_delete parameter. The on_delete is one of the parameter which helps to perform database-related task efficiently. This parameter is used when a relationship is established in Django. The on_delete parameter allows us to work with the foreign key.
I went to the folder of migrations and deleted the first name and last name columns inside the
0001_initial.py
You shouldn't do that. This will indeed prevent Django from creating the columns. But now it will each time query under the impression that the columns are there. Furthermore if you make migrations again, it will try to add extra columns.
You should simply use the method resolution order (MRO) and set the field to None
:
class usermodel(AbstractUser):
first_name = None
last_name = None
def __str__(self):
return self.username
You probably also better remove the table in the database and the migration file and make migrations again, and migrate the database.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With