How to create user from django shell

前端 未结 7 465
借酒劲吻你
借酒劲吻你 2021-01-30 04:13

When i create user from django-admin user password\'s are encrypted . but when i create user from django shell user-pasword is saved in plain text . Example :

相关标签:
7条回答
  • 2021-01-30 04:23

    There are couple of way to set password for a django user object from django-shell.

    user = User(username="django", password = "secret")
    user.save()
    

    This will store encrypted password.

    user = User(username="django")
    user.set_password("secret")
    user.save()
    

    This will store encrypted password.

    But,

    user = User(username="django")
    user.password="secret"
    user.save()
    

    This will store plain text password. There is no hashing / encryptions applied since you are modifying the property directly.

    0 讨论(0)
  • 2021-01-30 04:26

    You should not create the user via the normal User(...) syntax, as others have suggested. You should always use User.objects.create_user(), which takes care of setting the password properly.

    user@host> manage.py shell
    >>> from django.contrib.auth.models import User
    >>> user=User.objects.create_user('foo', password='bar')
    >>> user.is_superuser=True
    >>> user.is_staff=True
    >>> user.save()
    
    0 讨论(0)
  • 2021-01-30 04:26

    Answer for those using django 1.9 or greater since from django.contrib.auth.models import User has been deprecated (possibly even earlier) but definitely by 1.9.

    Instead do: in bash:

    python manage.py shell
    

    In the python shell to create a user with a password:

    from django.apps import apps
    User = apps.get_model('user', 'User')
    me = User.objects.create(first_name='john', email='johnsemail@email.com') # other_info='other_info', etc.
    me.set_password('WhateverIwant')  # this will be saved hashed and encrypted
    me.save()
    

    If coming from an API you should probably apply a Form as such:

    import json
    User = get_model('User')
    class UserEditForm(BaseModelForm):
            """This allows for validity checking..."""
    
            class Meta:
                model = User
                fields = [
                    'first_name', 'password', 'last_name',
                    'dob', # etc...
                ]
    # collect the data from the API:
    post_data = request.POST.dict()
    data = {
    'first_name': post_data['firstName'],
    'last_name': post_data['firstName'],
    'password': post_data['password'], etc.
    }
    dudette = User()  # (this is for create if its edit you can get the User by pk with User.objects.get(pk=kwargs.pk))
    form = UserEditForm(data, request.FILES, instance=dudette)
    if form.is_valid():
        dudette = form.save()
    else:
        dudette = {'success': False, 'message': unicode(form.errors)}
    return json.dumps(dudette.json())  # assumes you have a json serializer on the model called def json(self):
    
    0 讨论(0)
  • 2021-01-30 04:29

    You use user.set_password to set passwords in the django shell. I'm not even sure if directly setting the password via user.password would even work, since Django expects a hash of the password.

    The password field doesn't store passwords; it stores them as <algorithm>$<iterations>$<salt>$<hash>, so when it checks a password, it calculates the hash, and compares it. I doubt the user actually has a password whose calculated password hash is in <algorithm>$<iterations>$<salt>$<hash> form.

    If you get the json with all the information needed to create the User, you could just do

    User.objects.create_user(**data)
    

    assuming your passed json is called data.

    Note: This will throw an error if you have extra or missing items in data.

    If you really want to override this behavior, you can do

    def override_setattr(self,name,value):
        if name == 'password':
            self.set_password(value)
        else:
            super().__setattr__(self,name,value) #or however super should be used for your version
    
    User.__setattr__ = override_setattr
    

    I haven't tested this solution, but it should work. Use at your own risk.

    0 讨论(0)
  • 2021-01-30 04:38

    To create Users execute:

    $ python manage.py shell
    >>>> from django.contrib.auth.models import User
    >>>> user = User.objects.create_user('USERNAME', 'MAIL_NO_REQUIRED', 'PASSWORD')
    
    0 讨论(0)
  • 2021-01-30 04:39

    The fastest way creating of super user for django, type in shell:

    python manage.py createsuperuser
    
    0 讨论(0)
提交回复
热议问题