##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.







On Mon, Mar 6, 2017 at 2:37 PM, Focus Ifeanyi <[email protected]> wrote:

> 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.
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> 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
> <https://groups.google.com/d/msgid/django-users/7c9f19a0-0e25-4529-9353-6e69b1d9e695%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
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/CAE6v7ocxKgRkfh9Ez7UaRSGOByKph2vM5GWPCo8DEbq%3DwZ6u3g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to