Django - Multiple User Profiles

前端 未结 3 459
小鲜肉
小鲜肉 2021-01-29 21:27

Initially, I started my UserProfile like this:

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

class UserProfile(models.Model):
    use         


        
相关标签:
3条回答
  • 2021-01-29 21:38

    Could be worth to try using a through field. The idea behind it is to use the UserProfile model as through model for the CorpProfile or IndivProfile models. That way it is being created as soon as a Corp or Indiv Profile is linked to a user:

    from django.db import models
    from django.contrib.auth.models import User
    
    class UserProfile(models.Model):
        user = models.ForeignKey(User)
        profile = models.ForeignKey(Profile, related_name='special_profile')
    
    class Profile(models.Model):
        common_property=something
    
    class CorpProfile(Profile):
        user=models.ForeignKey(User, through=UserProfile)
        corp_property1=someproperty1
        corp_property2=someproperty2
    
    class IndivProfile(Profile):
        user=models.ForeignKey(User, through=UserProfile, unique=true)
        indiv_property1=something
        indiv_property2=something
    

    I think that way it should be possible to set AUTH_PROFILE_MODULE = 'accounts.UserProfile', and every time you create either a CorpProfile or a IndivProfile that is linked to a real user a unique UserProfile model is created. You can then access that with db queries or whatever you want.

    I haven't tested this, so no guarantees. It may be a little bit hacky, but on the other side i find the idea quite appealing. :)

    0 讨论(0)
  • 2021-01-29 21:42

    You can do this in following way. Have a profile which will contains common fields which are necessary in both profiles. And you have already done this by creating class UserProfile.

    class UserProfile(models.Model):
        user = models.ForeignKey(User)
        # Some common fields here, which are shared among both corporate and individual profiles
    
    class CorporateUser(models.Model):
        profile = models.ForeignKey(UserProfile)
        # Corporate fields here
    
        class Meta:
            db_table = 'corporate_user'
    
    class IndividualUser(models.Model):
        profile = models.ForeignKey(UserProfile)
        # Individual user fields here
    
       class Meta:
            db_table = 'individual_user'
    

    There is no rocket science involved here. Just have a keyword which will distinguish between corporate profile or individual profile. E.g. Consider that the user is signing up. Then have a field on form which will differentiate whether the user is signing up for corporate or not. And Use that keyword(request parameter) to save the user in respective model.

    Then later on when ever you want to check that the profile of user is corporate or individual you can check it by writing a small function.

    def is_corporate_profile(profile):
        try:
            profile.corporate_user
            return True
        except CorporateUser.DoesNotExist:
            return False
    
    # If there is no corporate profile is associated with main profile then it will raise `DoesNotExist` exception and it means its individual profile
    # You can use this function as a template function also to use in template
    {% if profile|is_corporate_profile %}
    

    Hope this will lead you some where. Thanks!

    0 讨论(0)
  • 2021-01-29 21:57

    I have done it this way.

    PROFILE_TYPES = (
        (u'INDV', 'Individual'),
        (u'CORP', 'Corporate'),
    )
    
    # used just to define the relation between User and Profile
    class UserProfile(models.Model):
        user = models.ForeignKey(User)
        profile = models.ForeignKey('Profile')
        type = models.CharField(choices=PROFILE_TYPES, max_length=16)
    
    # common fields reside here
    class Profile(models.Model):
        verified = models.BooleanField(default=False)
    

    I ended up using an intermediate table to reflect the relation between two abstract models, User which is already defined in Django, and my Profile model. In case of having attributes that are not common, I will create a new model and relate it to Profile.

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