Search code examples
pythondjangodjango-modelsuser-mode-linux

How can i add more table fields on already developed Django User table Database


I want to add more fields in User Data table so Please Explain the right method to done this.


Solution

  • it is very simple, in your models.py in the User model just simply add another field,

    example:

    class User(models.Model):
        name = models.CharField(max_length=100)
        phone = models.IntegerField()
        created_date = models.DateTimeField(default=timezone.now)
        modified_date = models.DateTimeField(default=timezone.now)
    

    lets add the field email:

    class User(models.Model):
        name = models.CharField(max_length=100)
        phone = models.IntegerField()
        email = models.CharField(max_length=100)
        created_date = models.DateTimeField(default=timezone.now)
        modified_date = models.DateTimeField(default=timezone.now)
    

    then run this command in your terminal:

    python manage.py makemigrations
    

    and it will display the alter you are about to make and then run the command:

    python manage.py migrate
    

    and the new field will be added.I hope this helps you

    Cheers!