S
S
Sergey Nekrasov2016-03-02 14:41:48
Django
Sergey Nekrasov, 2016-03-02 14:41:48

How to properly model the users table?

There are 2 categories of users,
"Master" and "Customer"
if the user has the type "Master", then
he must have his own special field, for example "Electrician",
and if the type is "Customer", then it is logical that he has this field should not.
Currently implemented like this:

USER_CATEGORY = ((1,"Customer"), (2,"Master"))

class UserCategory(models.Model):
    choice=models.IntegerField(choices=USER_CATEGORY, db_index=True)
    def __str__(self):
        return str(self.choice)

class MasterCategory(models.Model):
    category_name=models.CharField(max_length=200, db_index=True, )
    description=models.TextField()
    def __str__(self):
        return self.category_name

class User(models.Model):
    name=models.CharField(max_length=200)
    user_category=models.ForeignKey(UserCategory)
    master_category=models.ForeignKey(MasterCategory, null=True, blank=True)
    def __str__(self):
        return self.name

that is, I have here a common table for everyone, some fields in which refer to other tables, but
with such an implementation, the "Customer" can still choose a specialization intended only for the user of the "Master" category.
Tell me how to make these connections correctly, and yet, because somewhere you need to define admin accounts?
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bpe, 2016-05-05
@bpe

Make one general ABSTRACT model in which there will be all COMMON fields for the Master and the Customer. Then inherit from it the MasterUser and CustomerUser tables, which will already have their own fields, those that are needed for a particular type of user.
For an abstract model, do not forget to specify:

class Meta:
        abstract = True

And it's better not to make a model named User.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question