2011-12-03 64 views
0

我有多個數據庫。傳統的Mongo db,'mongo'和我正在遷移到的'PostgreSQL db','sql'。我正試圖移動所有用戶和他們的權限。如何將django身份驗證從mongo遷移到SQL? (Mongo上的ManyRelatedManger)

在代碼中,我已經將權限從'mongo'移動到'sql'。

users = User.objects.using('mongo').all().values('username') 
for user in users: 
    u = User.objects.db_manager('sql').create_user(user.username, user.email, user.password) 
    # Where the ManyRelatedManager trouble begins 
    # This error is "'dict' object has no attribute 'user_permissions'" 
    u.user_permissions.add(user.user_permissions) 
    # This error is "This query is not supported by the database." on the .all() 
    old_permissions = user.user_permissions.all() 
    for p in old_permissions: 
     new_permission = Permission.objects.using('sql').get(name=p.name) 
     u.user_permissions.add(new_permission) 
     u.save() 

由於Mongo不喜歡連接,我如何訪問用戶的Permission對象?

+0

我的解決方法是將所有Permssions授予超級用戶。我從來沒有正確地解決我如何訪問使用mongo的用戶的權限對象。 (對文檔進行迭代並填充到SQL-land中太模糊,無法成爲解決方案。) – user140314

回答

1

因此,如果我在我的django項目(sqlite3)中查看,在同步身份驗證應用程序時會創建7個表。我想看看如何每次在一個標準的Django應用程序

auth_group   
auth_group_permissions 
auth_message 
auth_permission 
auth_user 
auth_user_groups 
auth_user_user_permissions 

如果我顯示了他們的方案中創建的,它看起來像這樣:

CREATE TABLE "auth_group" (
    "id" integer NOT NULL PRIMARY KEY, 
    "name" varchar(80) NOT NULL UNIQUE 
); 
CREATE TABLE "auth_group_permissions" (
    "id" integer NOT NULL PRIMARY KEY, 
    "group_id" integer NOT NULL, 
    "permission_id" integer NOT NULL REFERENCES "auth_permission" ("id"), 
    UNIQUE ("group_id", "permission_id") 
); 
CREATE TABLE "auth_message" (
    "id" integer NOT NULL PRIMARY KEY, 
    "user_id" integer NOT NULL REFERENCES "auth_user" ("id"), 
    "message" text NOT NULL 
); 
CREATE TABLE "auth_permission" (
    "id" integer NOT NULL PRIMARY KEY, 
    "name" varchar(50) NOT NULL, 
    "content_type_id" integer NOT NULL, 
    "codename" varchar(100) NOT NULL, 
    UNIQUE ("content_type_id", "codename") 
); 
CREATE TABLE "auth_user" (
    "id" integer NOT NULL PRIMARY KEY, 
    "username" varchar(30) NOT NULL UNIQUE, 
    "first_name" varchar(30) NOT NULL, 
    "last_name" varchar(30) NOT NULL, 
    "email" varchar(75) NOT NULL, 
    "password" varchar(128) NOT NULL, 
    "is_staff" bool NOT NULL, 
    "is_active" bool NOT NULL, 
    "is_superuser" bool NOT NULL, 
    "last_login" datetime NOT NULL, 
    "date_joined" datetime NOT NULL 
); 
CREATE TABLE "auth_user_groups" (
    "id" integer NOT NULL PRIMARY KEY, 
    "user_id" integer NOT NULL, 
    "group_id" integer NOT NULL REFERENCES "auth_group" ("id"), 
    UNIQUE ("user_id", "group_id") 
); 
CREATE TABLE "auth_user_user_permissions" (
    "id" integer NOT NULL PRIMARY KEY, 
    "user_id" integer NOT NULL, 
    "permission_id" integer NOT NULL REFERENCES "auth_permission" ("id"), 
    UNIQUE ("user_id", "permission_id") 
); 

使用執行syncdb在創建這些表的啓用身份驗證應用程序後重新啓動新數據庫,然後遍歷mongo版本中的文檔。對於每個文檔,在SQL-land中填充適當的表。