0

I have this model

class MyUser(AbstractBaseUser):
    ##username =models.U
    email=models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )
    date_of_birth=models.DateField()
    picture =models.ImageField(upload_to='images/users',null=True,verbose_name="")
    is_active =models.BooleanField(default=True)
    phone_number = models.IntegerField(max_length=12,unique=True,null=False,verbose_name='phone')
    is_admin = models.BooleanField(default=False)
    #credits =models.PositiveIntegerField(default=100)
    linkedin_token=models.TextField(blank=True ,default='')
    expiry_date=models.DateTimeField(null=True, blank=True)
    objects=UserManger()

when i run python manage.py makemigrations I got this error

WARNINGS: Accounts.MyUser.phone_number: (fields.W122) 'max_length' is ignored when used with IntegerField. HINT: Remove 'max_length' from field It is impossible to add a non-nullable field 'phone_number' to myuser without specifying a default. This is because the database needs something to populate existing rows.

can any one help me ?

Zakaria Zhlat
  • 320
  • 1
  • 6
  • 21
  • 1
    remove the `max_length=12` from your phone_number IntegerField arguments. Set null to True to avoid the second part of the error. Or set a default to zero or all nines or similar if you don't want to remove the required constraint. – AMG Apr 19 '22 at 18:35
  • 1
    use a [Validator](https://docs.djangoproject.com/en/4.0/ref/validators/) if you want to validate on save. I don't know what types of phone number you are storing but you could *maybe* use a [MinValueValidator] (https://docs.djangoproject.com/en/4.0/ref/validators/#minvaluevalidator). May also want to read https://stackoverflow.com/a/24353813/4872140 (tl;dr store them as CharField). – AMG Apr 19 '22 at 18:40

0 Answers0