I have created my own custom user model in django and specified the same in settings.py :
AUTH_USER_MODEL = 'userprofile.User'
After this, I created User as an abstract class since I wanted to have two separate classes derived from this class namely - Vendor and Customer . This will create two separate database tables.
The problem occurs when I created another class - ProductReview . This class needs a foreign key to the user who added the review and this user can be either a Vendor or a Customer . I am not sure how to put this foreign key constraint here, because settings.AUTH_USER_MODEL will not work, as User is an abstract class.
Following is the class structure:
class UserManager(BaseUserManager):
class User(PermissionsMixin, AbstractBaseUser):
class Meta:
abstract = True
class Vendor(User):
class Customer(User):
class ProductReview(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL, related_name='reviews', null=True, blank=True)
Edit - I went ahead with three different tables for each of the models.
The declaration of user field in ProductReview
model class will map with the USER
model. To define whether it is a Vendor
or a Customer
you need to declare another field probably integer field with mapping declared as
TYPE_OF_USER_MAPPING = {
1: 'Vendor',
2: 'Customer'
}
and then create a query to check in which model Vendor
or Customer
the original USER
is.
For your case I would consider changing my modeling. This how I would do it:
User
as the auth user model for Django. Vendor
and a Customer
class with one-to-one relationship to the User
model in order to extend it. In this way, a you can have a ProductReview
to reference a User
, who could be either a Vendor
or a Customer
, or you can reference Customer
s only if you wish.
See more on Django documentation: Extending the User model .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.