Tuesday, July 19, 2016

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

On Jul 19, 2016 5:15 AM, "Jeff Willette" <jrwillette88@gmail.com> wrote:
>
> 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?
>

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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciXhCaFuiLiwZzq2w7knY8c%3DhMX7qHRPi2r12wNzCSYLQA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment