Django中的多种用户类型 [英] Multiple User Types In Django

查看:221
本文介绍了Django中的多种用户类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django的新手,正在尝试创建具有两种用户类型(自由职业者和客户)的应用。我了解如何创建用户个人资料类,并且它对我很有效:

I am new to Django and trying to create an App with two User Types (Freelancers and Customers). I understand how to create a User profile Class and it works well for me:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    description = models.CharField(max_length=100, default='')
    country = models.CharField(max_length=100, default='')
    website = models.URLField(default='')
    phone = models.IntegerField(default=0)

def create_profile(sender, **kwargs):
    if kwargs['created']:
        user_profile = UserProfile.objects.create(user=kwargs['instance'])


post_save.connect(create_profile, sender=User)

这对一个用户类型的用户来说效果很好。但是,现在我正在构建具有两种类型的用户(自由职业者和客户)的应用程序,什么是完成此任务的最佳方法?这两个用户将具有不同的视图和信息。我应该:

This works well for me on a one user type user. But now I am building an app with 2 types of users (freelancers and customers), what is the best approach to get this done. Both users will have different view and info. Should I:


  1. 创建2个不同的应用程序,然后分别进行正常注册和登录。

  2. 如果执行上述操作,希望自由职业者登录后不会进入客户视图。

  3. 如果我决定使用一个应用程序并为其建模,如何将用户类型添加到用户配置文件中。
    请我需要逐步的初学者方法,或指向相关资源的链接。
    谢谢。


推荐答案

您可以尝试以下方法:

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

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

    class Meta:
        db_table = 'freelancer'

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

   class Meta:
        db_table = 'customer'

然后可以从UserProfile中拥有任意数量的用户

You can then have as many Users as you want from the UserProfile.

这篇关于Django中的多种用户类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆