Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3350804
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:49:53+00:00 2026-05-18T01:49:53+00:00

Instead of using django’s auth module I’ve used my own and already regret it

  • 0

Instead of using django’s auth module I’ve used my own and already regret it a lot.

In an effort to rectify the situation, I’m trying to migrate the data from my User model to django.auth.models.User.

I’ve created a data migration as follows:

def forwards(self, orm):
    """Migrate user information from mooi User model to auth User model."""

    OldUser = orm['mooi.User']
    User = orm['auth.User']
    Profile = orm['mooi.Profile']

    oldUsers = OldUser.objects.all()
    for oldUser in oldUsers:
        newUser = User.objects.create_user(username=oldUser.id, email=oldUser.email, password=oldUser.password)
        # ...more irrelevant code follows...

When I run the migration, I get this error (traceback):

#...irrelevant traceback precedes...
File "[projdir]/mooi/migrations/0005_from_mooi_users_create_auth_users_with_profiles.py", line 18, in forwards
    newUser = User.objects.create_user(username=oldUser.id, email=oldUser.email, password=oldUser.password)
  File "[virtual_env_dir]lib/python2.6/site-packages/south/orm.py", line 397, in __getattr__
    return getattr(self.real, name)
AttributeError: 'Manager' object has no attribute 'create_user'

Upon further investigation, I discovered that the Manager that was being referred to was of time south.orm.NoDryRunManager which explains the error.

Now, the reason I even need create_user is to create a password hash that django.contrib.auth will understand.

Having said all that, how do I go about doing this? What’s the most elegant solution given the hole I’m in?!

Thanks in advance.

Update 1

As suggested by stevejalim, I tried to use User‘s set_password(...) as follows:

newUser.set_password(raw_password=oldUser.password)
newUser.save()

However that failed with this error:

File "[projdir]/mooi/migrations/0005_from_mooi_users_create_auth_users_with_profiles.py", line 21, in forwards
    newUser.set_password(raw_password=oldUser.password)
AttributeError: 'User' object has no attribute 'set_password'

I did find a hint in the south documentation which states that:

South doesn’t freeze every aspect of a
model; for example, it doesn’t
preserve new managers, or custom model
methods
, as these would require
serialising the python code that runs
those method (and the code that
depends on, and so forth).

If you want custom methods in your
migration, you’ll have to copy the
code in, including any imports it
relies on to work
. Remember, however,
for every import that you add, you’re
promising to keep that import valid
for the life for the migration.

I guess the question remains, what’s the best/safest way of doing this? Copy the set_password(...) method over? Create a function that hashes the password for me? Any other ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:49:54+00:00Added an answer on May 18, 2026 at 1:49 am

    Ok, it turns out that South doesn’t freeze methods at all, so calling any model methods is of no use.

    The way I solved this was by coping and modifying the code in contrib.auth that generates passwords.

    Here’s how the final migration looks like:

    # encoding: utf-8
    import datetime
    from south.db import db
    from south.v2 import DataMigration
    from django.db import models
    
    class Promise(object):
        """
        This is just a base class for the proxy class created in
        the closure of the lazy function. It can be used to recognize
        promises in code.
        """
        pass
    
    def lazy(func, *resultclasses):
        """
        Turns any callable into a lazy evaluated callable. You need to give result
        classes or types -- at least one is needed so that the automatic forcing of
        the lazy evaluation code is triggered. Results are not memoized; the
        function is evaluated on every access.
        """
    
        class __proxy__(Promise):
            """
            Encapsulate a function call and act as a proxy for methods that are
            called on the result of that function. The function is not evaluated
            until one of the methods on the result is called.
            """
            __dispatch = None
    
            def __init__(self, args, kw):
                self.__func = func
                self.__args = args
                self.__kw = kw
                if self.__dispatch is None:
                    self.__prepare_class__()
    
            def __reduce__(self):
                return (
                    _lazy_proxy_unpickle,
                    (self.__func, self.__args, self.__kw) + resultclasses
                )
    
            def __prepare_class__(cls):
                cls.__dispatch = {}
                for resultclass in resultclasses:
                    cls.__dispatch[resultclass] = {}
                    for (k, v) in resultclass.__dict__.items():
                        # All __promise__ return the same wrapper method, but they
                        # also do setup, inserting the method into the dispatch
                        # dict.
                        meth = cls.__promise__(resultclass, k, v)
                        if hasattr(cls, k):
                            continue
                        setattr(cls, k, meth)
                cls._delegate_str = str in resultclasses
                cls._delegate_unicode = unicode in resultclasses
                assert not (cls._delegate_str and cls._delegate_unicode), "Cannot call lazy() with both str and unicode return types."
                if cls._delegate_unicode:
                    cls.__unicode__ = cls.__unicode_cast
                elif cls._delegate_str:
                    cls.__str__ = cls.__str_cast
            __prepare_class__ = classmethod(__prepare_class__)
    
            def __promise__(cls, klass, funcname, func):
                # Builds a wrapper around some magic method and registers that magic
                # method for the given type and method name.
                def __wrapper__(self, *args, **kw):
                    # Automatically triggers the evaluation of a lazy value and
                    # applies the given magic method of the result type.
                    res = self.__func(*self.__args, **self.__kw)
                    for t in type(res).mro():
                        if t in self.__dispatch:
                            return self.__dispatch[t][funcname](res, *args, **kw)
                    raise TypeError("Lazy object returned unexpected type.")
    
                if klass not in cls.__dispatch:
                    cls.__dispatch[klass] = {}
                cls.__dispatch[klass][funcname] = func
                return __wrapper__
            __promise__ = classmethod(__promise__)
    
            def __unicode_cast(self):
                return self.__func(*self.__args, **self.__kw)
    
            def __str_cast(self):
                return str(self.__func(*self.__args, **self.__kw))
    
            def __cmp__(self, rhs):
                if self._delegate_str:
                    s = str(self.__func(*self.__args, **self.__kw))
                elif self._delegate_unicode:
                    s = unicode(self.__func(*self.__args, **self.__kw))
                else:
                    s = self.__func(*self.__args, **self.__kw)
                if isinstance(rhs, Promise):
                    return -cmp(rhs, s)
                else:
                    return cmp(s, rhs)
    
            def __mod__(self, rhs):
                if self._delegate_str:
                    return str(self) % rhs
                elif self._delegate_unicode:
                    return unicode(self) % rhs
                else:
                    raise AssertionError('__mod__ not supported for non-string types')
    
            def __deepcopy__(self, memo):
                # Instances of this class are effectively immutable. It's just a
                # collection of functions. So we don't need to do anything
                # complicated for copying.
                memo[id(self)] = self
                return self
    
        def __wrapper__(*args, **kw):
            # Creates the proxy object, instead of the actual value.
            return __proxy__(args, kw)
    
        return wraps(func)(__wrapper__)
    
    
    # code to encrypt passwords borrowed from django 1.2.1:
    def smart_str(s, encoding='utf-8', strings_only=False, errors='strict'):
        """
        Returns a bytestring version of 's', encoded as specified in 'encoding'.
    
        If strings_only is True, don't convert (some) non-string-like objects.
        """
        if strings_only and isinstance(s, (types.NoneType, int)):
            return s
        if isinstance(s, Promise):
            return unicode(s).encode(encoding, errors)
        elif not isinstance(s, basestring):
            try:
                return str(s)
            except UnicodeEncodeError:
                if isinstance(s, Exception):
                    # An Exception subclass containing non-ASCII data that doesn't
                    # know how to print itself properly. We shouldn't raise a
                    # further exception.
                    return ' '.join([smart_str(arg, encoding, strings_only,
                            errors) for arg in s])
                return unicode(s).encode(encoding, errors)
        elif isinstance(s, unicode):
            return s.encode(encoding, errors)
        elif s and encoding != 'utf-8':
            return s.decode('utf-8', errors).encode(encoding, errors)
        else:
            return s
    
    def get_hexdigest(algorithm, salt, raw_password):
        """
        Returns a string of the hexdigest of the given plaintext password and salt
        using the given algorithm ('md5', 'sha1' or 'crypt').
        """
        raw_password, salt = smart_str(raw_password), smart_str(salt)
        if algorithm == 'crypt':
            try:
                import crypt
            except ImportError:
                raise ValueError('"crypt" password algorithm not supported in this environment')
            return crypt.crypt(raw_password, salt)
        # The rest of the supported algorithms are supported by hashlib, but
        # hashlib is only available in Python 2.5.
        try:
            import hashlib
        except ImportError:
            if algorithm == 'md5':
                import md5
                return md5.new(salt + raw_password).hexdigest()
            elif algorithm == 'sha1':
                import sha
                return sha.new(salt + raw_password).hexdigest()
        else:
            if algorithm == 'md5':
                return hashlib.md5(salt + raw_password).hexdigest()
            elif algorithm == 'sha1':
                return hashlib.sha1(salt + raw_password).hexdigest()
        raise ValueError("Got unknown password algorithm type in password.")
    
    def get_encrypted_password(raw_password):
        import random
        algo = 'sha1'
        salt = get_hexdigest(algo, str(random.random()), str(random.random()))[:5]
        hsh = get_hexdigest(algo, salt, raw_password)
        return '%s$%s$%s' % (algo, salt, hsh)
    
    
    class Migration(DataMigration):
    
        def forwards(self, orm):
            """Migrate user information from mooi User model to auth User model."""
    
            OldUser = orm['mooi.User']
            User = orm['auth.User']
            Profile = orm['mooi.Profile']
    
            oldUsers = OldUser.objects.all()
            for oldUser in oldUsers:
                newUser = User(username=oldUser.id, email=oldUser.email)
                newUser.first_name = oldUser.name
                newUser.save()
                newUser.password = get_encrypted_password(oldUser.password)
                newUser.save()
                newUserProfile = Profile(user=newUser)
                newUserProfile.phone = oldUser.phone
                newUserProfile.credits = oldUser.credits
                newUserProfile.transaction_status = oldUser.transaction_status
                newUserProfile.location = oldUser.location
                newUserProfile.save()
                assert oldUser.id == newUser.username, \
                    "Old user: %s, is not equal to: %s" % (oldUser.id, newUser.username)
                assert oldUser.name == newUser.first_name, \
                    "Names don't match, old: %s, new: %s" % (oldUser.name, newUser.first_name)
                assert oldUser.email == newUser.email, \
                    "Emails don't match, old: %s, new: %s" % (oldUser.email, newUser.email)
                assert oldUser.phone == newUserProfile.phone, \
                    "Phones don't match, old: %s, new: %s" % (oldUser.phone, newUserProfile.phone)
                assert oldUser.credits == newUserProfile.credits, \
                    "Credits don't match, old: %s, new: %s" % (oldUser.credits, newUserProfile.credits)
                assert oldUser.transaction_status == newUserProfile.transaction_status, \
                    "Trans. status don't match, old: %s, new: %s" % (oldUser.transaction_status, newUserProfile.transaction_status)
                assert oldUser.location == newUserProfile.location, \
                    "Locations don't match: old: %s, new: %s" % (oldUser.location == newUserProfile.location)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to Authenticate users by their emails instead of username. I'm using django-userena
I would like to start using Django with MYSQL, instead of sqlite all the
I'm trying create a simple background periodic task using Django-Celery-RabbitMQ combination. I installed Django
Instead using the google search appliance crawler for index content, im using a query
Instead using of: if ( ! $('#XX').is(':visible) ) Is there a value called invisible
Instead of using an interface like this: public interface IStartable { void Start(); void
Instead of using an external web-based Mercurial host, I want to set one up
instead of using getParameterByName('Field', PostData) (PostData == $('form').serialize();) I would like to write PostData.Field,
I am using jQuery with ASP.NET in a project. Instead of using ASP.NET Ajax,
Can I authenticate with just Google account username and password instead of using OAuth?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.