user10753047
user10753047

Reputation:

How to set email field in Django?

Hi I'm making a user model in Django but getting an error regarding email field and the unique=True also not working on the email field.

class User(models.Model):
    user_name   = models.CharField(max_length=50,unique=True)
    first_name  = models.CharField(max_length=50)
    last_name   = models.CharField(max_length=50)
    User_email  = models.EmailField(max_length=70,blank=True,unique=True)
    password    = models.CharField(max_length=12)

The error I am getting when I am trying to run the command "python manage.py makemigrations":

You are trying to add a non-nullable field 'first_name' to user without a default; we can't do that (the database needs something to populate existing rows). Please select a fix: 1) Provide a one-off default now (will be set on all existing rows with a null value for this column) 2) Quit, and let me add a default in models.py Select an option:

Upvotes: 0

Views: 9470

Answers (1)

Matthew Gaiser
Matthew Gaiser

Reputation: 4763

Basically, you already have users in your database. When you add a new field to User, there ends up being a blank spot in the database for the existing users.

However, your code is such that a blank spot is not permitted. As your code is currently, there must be a value in that spot for all users.

Your options:

1. Wipe your database

If you aren't far long in the development process, just reset your database to have no users. Then all should work properly.

2. Let it be blank/null for some users and fix it later

Add one of the following to your User_Id declaration. default='DEFAULT VALUE', blank=True or null=TrueThat will let you continue along, but then you will need to fix it later for the older users.

I will also point out that your error is not about the email (User_email)field, but rather the User_id field. Just delete that entirely. Django models already come with a built in system for managing primary keys. It is the pk.

Upvotes: 2

Related Questions