Migrating existing auth.User data to new Django 1.5 custom user model?

前端 未结 5 1176
余生分开走
余生分开走 2020-11-30 18:59

I\'d prefer not to destroy all the users on my site. But I want to take advantage of Django 1.5\'s custom pluggable user model. Here\'s my new user model:

         


        
5条回答
  •  粉色の甜心
    2020-11-30 19:56

    I got tired of struggling with South so I actually ended up doing this differently and it worked out nicely for my particular situation:

    First, I made it work with ./manage.py dumpdata, fixing up the dump, and then ./manage.py loaddata, which worked. Then I realized I could do basically the same thing with a single, self-contained script that only loads necessary django settings and does the serialization/deserialization directly.

    Self-contained python script

    ## userconverter.py ##
    
    import json
    from django.conf import settings
    
    settings.configure(
        DATABASES={ 
                # copy DATABASES configuration from your settings file here, or import it directly from your settings file (but not from django.conf.settings) or use dj_database_url
                },
        SITE_ID = 1, # because my custom user implicates contrib.sites (which is why it's in INSTALLED_APPS too)
        INSTALLED_APPS = ['django.contrib.sites', 'django.contrib.auth', 'myapp'])
    
    # some things you have to import after you configure the settings
    from django.core import serializers
    from django.contrib.auth.models import User
    
    # this isn't optimized for huge amounts of data -- use streaming techniques rather than loads/dumps if that is your case
    old_users = json.loads(serializers.serialize('json', User.objects.all()))
    for user in old_users:
        user['pk'] = None
        user['model'] = "myapp.siteuser"
        user['fields']["site"] = settings['SITE_ID']
    
    for new_user in serializers.deserialize('json', json.dumps(old_users)):
        new_user.save()
    

    With dumpdata/loaddata

    I did the following:

    1) ./manage.py dumpdata auth.User

    2) Script to convert auth.user data to new user. (or just manually search and replace in your favorite text editor or grep) Mine looked something like:

    def convert_user_dump(filename, site_id):
        file = open(filename, 'r')
        contents = file.read()
        file.close()
        user_list = json.loads(contents)
        for user in user_list:
            user['pk'] = None  # it will auto-increment
            user['model'] = "myapp.siteuser"
            user['fields']["site"] = side_id
        contents = json.dumps(user_list)
        file = open(filename, 'w')
        file.write(contents)
        file.close()
    

    3) ./manage.py loaddata filename

    4) set AUTH_USER_MODEL

    *Side Note: One critical part of doing this type of migration, regardless of which technique you use (South, serialization/modification/deserialization, or otherwise) is that as soon as you set AUTH_USER_MODEL to your custom model in the current settings, django cuts you off from auth.User, even if the table still exists.*

提交回复
热议问题