[Django]-Set non-required fields from django.contrib.auth.User on Django

3👍

The django model fields are default to be NOT NULL

And if you want it allow NULL values, use null=True parameter.

If you want it allow empty value (” but not null), use blank=True parameter.

And if you want that field to be unique, set unique=True

class UserRegister(models.Model):

    first_name = models.CharField(max_length=150, null=True, blank=True)
    last_name = models.CharField(max_length=150, null=True, blank=True)
    email = models.EmailField(unique=True)

Maybe I misunderstood you question, you want to modify the User model fields.

And because you just can’t update the User model declaration, if you want to do so, you have the following choices:

  1. Make other fields to hold it, outside the user field.
  2. Inherits the model from User, not use combination, then override the field.

Hope it helps.

1👍

If you need to change what attributes are required or if you need other data about a user, I would suggest writing your own user model instead another model that links to Django’s. Docs are here, it’s not too difficult: https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-custom-user-model

Leave a comment