2012-07-13 53 views
0

我建立具有不同的用戶角色和做法,沒有達到正常的「創建一個用戶配置用戶信號」我第一次真正的Django應用程序。你們能幫我嗎?的Django 1.4:創建用戶配置+用戶作爲一個原子操作,角色和管理整合

爲了讓更多的上下文,這裏是從功能的角度要求:

  • 新用戶從管理員只加了,會被非高科技薩維人來完成的,所以我需要一個用戶創建流量直觀且簡單。
  • 每個用戶都有一個角色(經理,員工,推銷員等),具有不同的需求和領域。
  • 用戶列表需要同時顯示用戶信息和角色/配置文件信息(登錄,電子郵件,名稱,並在配置文件中的額外信息)

初始方法:

所以用這個裝備,我按照推薦的方法創建UserProfile 1對1對象鏈接到用戶,爲UserProfile提供一個選擇字段,其中設置了角色(用於瞭解調用get_profile()時處理的內容)和子類UserProfile進入ManagerUserProfile,EmployeeUserProfile等。

問題:

這適用於我在前端(管理員之外)的需求,但在創建用戶時設置創建UserProfile的信號毫無意義,因爲我不知道應根據何種類創建UserProfile用戶信息。

是我的目標是在創建一個特定用戶的一個原子的方式和它對應EmployeeUserProfile/ManagerUserProfile在同一時間,並具有整潔的管理員表示。

我的一些想法:

  • 隱藏UserAdmin和用戶配置文件管理員,創建EmployeeUserProfile /經理/等AdminModels和在線用戶模型。這樣,創建用戶的人只會看到一個「新管理器」鏈接及其相應的字段。但他們可能會創建用戶配置文件,而無需用戶?我怎樣才能使這個原子?我如何防止刪除用戶或確保他們提供所有必需的信息,然後才允許保存個人資料? - >這種方法的問題:我似乎無法內聯用戶,因爲它對UserProfile沒有PK(這是​​相反的方式)。

  • 同樣,隱藏UserAdmin,暴露子類的簡檔管理員,和反向信號。創建配置文件時,創建相應的用戶。但爲此,我需要能夠從配置文件管理員表單提供用戶字段(用戶名,密碼,電子郵件等)。

建議?

它是我的第一個應用程序,也許有一個很好的方法,但我還沒有找到它。

感謝您花時間閱讀本文。 乾杯, 澤塔。

回答

0

我建議您創建一個自定義窗體,自定義管理視圖並使用它在一個請求中創建用戶,並使用您需要的確切邏輯。您可以通過查看django自己的自定義用戶創建流程here來了解它是如何完成的。

+0

謝謝你,我會看看那個! – Zeta 2012-07-14 14:58:40

0

我終於找到了一種方法來實現我所需要的。它可能不是最乾淨的,它可以提供建議,但它可以幫助有類似問題的人。

因爲我只需要從管理員創建,我專注於此,並建立以下。

forms.py

class RequiredInlineFormSet(BaseInlineFormSet): 
    """ 
    Generates an inline formset that is required 
    """ 

    def _construct_form(self, i, **kwargs): 
     """ 
     Override the method to change the form attribute empty_permitted 
     """ 
     form = super(RequiredInlineFormSet, self)._construct_form(i, **kwargs) 
     form.empty_permitted = False 
     self.can_delete = False 
     return form 

models.py(我不使用信號來自動創建用戶創建一個配置文件)

class UserProfile(models.Model): 
    # This field is required. 
    user = models.OneToOneField(User) 

    # Other fields here 
    [.......] 

    USER_TYPES = (
     ('manager', 'Manager'), 
     ('employee', 'Employee'), 
    ) 

    user_type = models.CharField(blank=True, max_length=10, choices=USER_TYPES) 

    def __unicode__(self): 
     return self.user.username 


class EmployeeProfile(UserProfile): 
    [...] 

    def __init__(self, *args, **kwargs): 
     super(EmployeeProfile, self).__init__(*args, **kwargs) 
     self.user_type = 'employee' 

class ManagerProfile(UserProfile): 
    [...] 

    def __init__(self, *args, **kwargs): 
     super(ManagerProfile, self).__init__(*args, **kwargs) 
     self.user_type = 'manager' 


class Manager(User): 
    class Meta: 
     proxy = True 
     #app_label = 'auth' 
     verbose_name = 'manager' 
     verbose_name_plural = 'managers' 

    def save(self, *args, **kwargs): 
     self.is_staff = True 
     super(Manager, self).save(*args, **kwargs) # Call the "real" save() method. 
     g = Group.objects.get(name='Managers') 
     g.user_set.add(self) 


class Employee(User): 
    class Meta: 
     proxy = True 
     #app_label = 'auth' 
     verbose_name = 'employee' 
     verbose_name_plural = 'employees' 

    def save(self, *args, **kwargs): 
     self.is_staff = False 
     super(Employee, self).save(*args, **kwargs) # Call the "real" save() method. 
     g = Group.objects.get(name='Employees') 
     g.user_set.add(self) 

admin.py

class ManagerProfileAdmin(admin.StackedInline): 
    model = ManagerProfile 
    max_num = 1 
    extra = 1 
    formset = RequiredInlineFormSet 


class EmployeeProfileAdmin(admin.StackedInline): 
    model = EmployeeProfile 
    max_num = 1 
    extra = 1 
    formset = RequiredInlineFormSet 

class ManagerAdmin(UserAdmin): 
    """ 
    Options for the admin interface 
    """ 
    inlines = [ManagerProfileAdmin] 

    def queryset(self, request): 
     qs = super(UserAdmin, self).queryset(request) 
     qs = qs.filter(Q(userprofile__user_type='manager')) 

     return qs 


class EmployeeAdmin(UserAdmin): 
    """ 
    Options for the admin interface 
    """ 
    inlines = [EmployeeProfileAdmin] 

    def queryset(self, request): 
     qs = super(UserAdmin, self).queryset(request) 
     qs = qs.filter(Q(userprofile__user_type='employee')) 

     return qs 

admin.site.unregister(User) 
admin.site.register(Manager, ManagerAdmin) 
admin.site.register(Employee, EmployeeAdmin) 
相關問題