why am I able to create and save a user via the shell without any username?

23 views
Skip to first unread message

Jeff Willette

unread,
Jul 19, 2016, 8:15:44 AM7/19/16
to Django users
I have a custom user model via AbstractUser and I think the django-app `spirit` (which is a forum) has also modified my user model a bit. I am trying to write some tests and I do not know if this behavior is normal or not. 

Here is my model.py file that defines the user with the extra fields that I wanted personally...

from django.contrib.auth.models import AbstractUser


class CustomUserProfile(AbstractUser):
 
'''When I was defining the custom user model for the first time I had to migrate things in
 a very specific order for them to work. I had to migrate the main app first (the one with this model)
 then the sites app and then the main again and then everything else.'''



 topic
= models.ManyToManyField(MyModel, through='Info')


class Info(models.Model):
 user
= models.ForeignKey(CustomUserProfile, on_delete=models.CASCADE)
 topic
= models.ForeignKey(MyModel, on_delete=models.CASCADE)
 level
= models.IntegerField()

so when i start a shell I am able to do...

from topic.models import *
user
= CustomUserProfile()
user
.save()

and then if I pull all of my user objects I indeed see a blank user without a username. Is this normal behavior? Should I do something to change this?

James Schneider

unread,
Jul 20, 2016, 1:03:27 AM7/20/16
to django...@googlegroups.com

Running those commands in the shell does not execute the model-level validation you are expecting (checking for non-blank username and uniqueness in this case). You'll need to run that yourself when working directly with your user objects in the shell.

https://docs.djangoproject.com/en/1.9/ref/models/instances/#validating-objects

The database integrity checks still apply though. If you try to create a second user with an empty username, you should receive an IntegrityError upon running user.save().

-Jamez

Reply all
Reply to author
Forward
0 new messages