Home > Mobile >  How to remove fields from user creation form in django
How to remove fields from user creation form in django

Time:12-13

I am creating an app in django with postgressql. So I am creating my custom user model with own fields.BUt when I go to my database their are many fields like first name , last name, id. I dont want to see those field, I only want to see the fields that I am writing in my custom model. Edit: I am adding the model

class TableUsers(AbstractUser):
    username = None
 
    phoneNumberRegex = RegexValidator(r'^[0-9]*$', 'Enter a valid Phone number')
    user_id = models.AutoField(auto_created=True, primary_key=True)
    user_phone = models.CharField(validators = [phoneNumberRegex],max_length = 11, unique = True,)

    user_fname = models.CharField(max_length=40,null=True)
    user_lname = models.CharField(max_length=40,null=True)
    user_country = models.CharField(max_length=40,null=True)
   
    USERNAME_FIELD = 'user_phone'
    REQUIRED_FIELDS = []
    objects = CustomUserManager()

    def __str__(self):
        return str(self.user_phone)

CodePudding user response:

I think you have a misconception of what extending the user model should be.

First of all you should rename your model to something more explicit such as UserExtra or even User, it is totally fine.

Second of all you are adding field that are already provided for you by your Abstract User, so you don't have to add the last_name, first_name, id just the ones that are not already in the Abstract User.

Finally you should not try to remove the field provided by Django, they are tightly tied with Django itself and should not be removed.

See Abstract User

  • Related