User groups, and dififerent views / functions for each group

时间:2017-12-05 23:05:12

标签: django django-models django-forms django-templates django-views

Desired Output: Employee and Client user types. If registering through the main website the user will automatically be a Client. Client type users get a different menu/dashboard/portal whatever you wanna call that. For examples. Clients can book an appointment. Then for employees that usertype is set manually in django admin. AFterwards when they log in, they get a different dashboard, say one that displays their schedule and they can put available hours. This user doesnt even need to have a registration page it can all be done in admin.

From my limited understanding to bring this desired outcome to reality I must extend the userprofile? Or should I entirely create a custom userprofile using the AbstractUser abstraction?

Also where does django groups come in. And is all this User model extending even necessary? Can django groups be used entirely for this project?

class UserProfile(models.Model):
    user = models.ForeignKey(User)
    #define general fields

class Client(models.Model):
    profile = models.ForeignKey(UserProfile)
    #freelancer specific  fields

    class Meta:
        db_table = 'Client'

class Employee(models.Model):
    profile = models.ForeignKey(UserProfile)
    #Employee specific fields 

   class Meta:
        db_table = 'Employee'

Thanks in advance!!

1 个答案:

答案 0 :(得分:1)

您可以使用默认的django组

你的views.py中的

from django.contrib.auth.models import User
from django.contrib.auth.models import Group

# for inscription register the right group
def inscription(request):
    g = Group.objects.get(name='client')
    user = User()
    "etc"
    user.groups.add(g)
    user.save()

#To login
def loggedPage(request):
    userGroup = Group.objects.get(user=request.user).name
    if userGroup == 'client':
         "do some stuff"
    elif userGroup == 'employee':
         "do some other stuff"

警告:您必须使用

注册2个组
  

python manage.py shell

一个名为员工,另一个名为客户

您还可以使用管理来使用正确的组初始化数据库: 创建一个名为 yourapp / management / commands / DB_init.py 的文件(如果不存在则创建文件夹,并将其写入其中:

import os
os.environ["DJANGO_SETTINGS_MODULE"] = "YOURAPP.settings"
from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth.models import  Group


class Command(BaseCommand):
    def __init__(self, *args, **kwargs):
        super(Command, self).__init__(*args, **kwargs)
        group, created = Group.objects.get_or_create(name="client")
        if created:
            group.name = "client"
            group.save()

        group, created = Group.objects.get_or_create(name="employee")
        if created:
            group.name = "employee"
            group.save()

如果您已创建文件夹,请考虑放置" __ init __。py"在它们中将它们转换为python包。

您的文件应如下所示:

YOURAPP/
    __init__.py
    models.py
    management/
        __init__.py
        commands/
            __init__.py
            DB_init.py
    tests.py
    views.py

运行

python manage.py DB_init

瞧!