Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete the first name and last name columns in django user model?

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
like image 694
Mhmd Admn Avatar asked Oct 16 '20 14:10

Mhmd Admn


People also ask

How do I customize the Django user model?

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.

How to extend the Django user model with abstractuser?

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?

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.

What is the Django on_delete parameter?

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.


1 Answers

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.

like image 91
Willem Van Onsem Avatar answered Nov 14 '22 23:11

Willem Van Onsem