Requiring first_name and last_name in Django User model

Viewed 2382

In django.contrib.auth.models.User, both first_name and last_name fields have blank=True. How can I make them blank=False, null=False in my own model?

Here is my implementation, which basically follows the instructions of Extending the existing User model:

models.py

class Fellow(models.Model):

    user = models.OneToOneField(
        User,
        on_delete=models.CASCADE
    )

    first_name = models.CharField(
        _("first_name"),
        max_length=30,
    )
    last_name = models.CharField(
        _("last_name"),
        max_length=30,
    )

    # other fields omitted

from . import signals

signals.py

from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

from .models import Fellow


@receiver(post_save, sender=User)
def create_fellow_on_user_create(sender, instance, created, **kwargs):
    if created:
        Fellow.objects.create(user=instance)

However, I got an error when testing in python manage.py shell:

>>> f = Fellow.objects.create(username='username', password='passwd')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/db/models/query.py", line 392, in create
    obj = self.model(**kwargs)
  File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/db/models/base.py", line 571, in __init__
    raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
TypeError: 'username' is an invalid keyword argument for this function
2 Answers
Related