Multiple Users

26 views
Skip to first unread message

Focus Ifeanyi

unread,
Mar 6, 2017, 9:31:20 AM3/6/17
to Django users

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:

  • Create 2 different apps, and repeat the normal registeration and login for each.
  • If I do the above, hope the freelancers when logged in won't access customers view.
  • How do I add user type to the user profile if I decide to use one app and model for it.  Please I need a step by step beginner approach, or a link to relevant source. Thanks.

Lekan Wahab

unread,
Mar 6, 2017, 9:56:33 AM3/6/17
to django...@googlegroups.com
##This is one approach.
You can have a single user profile and a number of other users tied to the user profile via ForeignKey.

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'


Another approach(which i don't think is the best) would be to have a single UserProfile but with a table that identifies the type of user you have.
Something like this:

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
)
    
    ##The user type would be boolean and 0 by default. 0 could be a freelancer and 1 a Customer or the other way round.
    ##So, based on the type of user you're creating, you could just set a new value for user_type.
    user_type = models.BooleanField(default=0)

###This method would come in handy if both users have exactly the same features.
###But I'd rather go with the first method.






--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7c9f19a0-0e25-4529-9353-6e69b1d9e695%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages