0

I'm trying to make a custom User Model and have parameters like Date of Birth, current level and other such parameters. When I run python manage.py createsuperuser, it gives an error saying these values cannot be null. I don't want these parameters for the superuser account. WHat do I do?

I tried adding the following line to the class that inherits from the UserAdmin class exclude = ('date_of_birth', 'currentLevel', 'totalStars', 'badges')

This is the model fields

class Learner(AbstractBaseUser):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    email = models.EmailField(max_length=255, unique=True)
    date_of_birth = models.DateField(auto_now=False, auto_now_add=False)

    currentLevel = models.ForeignKey(SubLevelConcepts, related_name='currentLevel', on_delete=models.CASCADE)
    totalStars = models.IntegerField(default=0)
    badges = models.IntegerField(default=0)

    active = models.BooleanField(default=True)
    staff = models.BooleanField(default=False)
    superuser = models.BooleanField(default=False)

    objects = LearnerManager()

This is the admin class

class LearnerAdmin(UserAdmin):

    add_form = LearnerCreationForm
    form = LearnerChangeForm

    model = Learner

    exclude = ('date_of_birth', 'currentLevel', 'totalStars', 'badges')

    list_display = (
        'first_name',
        'last_name',
        'email',
        'staff',
        'active'
        )
    list_filter = (
        'email',
        'active'
    )
    fieldsets = (
        (None, {'fields': ('email', 'password',)}),
        ('Permissions', {'fields': ('staff', 'active')}),
    )

    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'first_name', 'last_name','password1', 'passsword2', 'staff', 'active')}
        )
    )

    search_fields = ('email',)
    ordering = ('id',)
    filter_horizontal = ()

This is the final error when i run python manage.py createsuperuser

django.db.utils.IntegrityError: (1048, "Column 'currentLevel_id' cannot be null")

I want the superuser to just take email and password and finish the job. I'll add the first_name and last_name later

1 Answers1

0

Add null=True and default=None to your ForeignKey field so it's not required, ie

class Learner(AbstractBaseUser):
    # ...
    currentLevel = models.ForeignKey(SubLevelConcepts, null=True, related_name='currentLevel', on_delete=models.CASCADE)

You may also want to use either models.PROTECT or models.SET_NULL instead of models.CASCADE - unless you really want your users accounts to be deleted when their matching SubLevelConcept is deleted, that is...

And you may also want to check the blank=True models field option FWIW.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118