Multiple User Types In Django

前端 未结 3 711
一生所求
一生所求 2021-01-15 09:34

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:

相关标签:
3条回答
  • 2021-01-15 09:54

    You could try this:

    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'
    

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

    0 讨论(0)
  • 2021-01-15 09:55

    You should need just use Groups Django mechanism - you need to create two groups freelancer and let say common and check whether user is in first or second group - then show him appropriate view

    To check whether user is in group you can use

    User.objects.filter(pk=userId, groups__name='freelancer').exists()
    
    0 讨论(0)
  • 2021-01-15 10:06

    You Could Try extending the Default Django Auth User like this Create an App with Account or Whatever name you like , then in models.py write like below

    class User(AbstractUser):
        is_head = models.BooleanField(default=False)
        is_staff = models.BooleanField(default=False)
        is_public = models.BooleanField(default=False)
    

    Add Auth Extended Model in Settings.py

    AUTH_USER_MODEL = 'accounts.User'
    

    Migrate your Account app and you are all set with Your User Extended Model.

    0 讨论(0)
提交回复
热议问题