B
B
bbquite2021-08-27 10:27:25
Django
bbquite, 2021-08-27 10:27:25

How to access django proxy model method?

There is a user model:

class User(AbstractBaseUser, PermissionsMixin):

    class Type(models.TextChoices):
        default = 'default', 'Стандартный Пользователь'
        vip = 'vip', 'Вип пользователь'


    email = models.EmailField(_('email address'), unique=True)
    phone = models.CharField(
        verbose_name='Телефон', blank=False, default='', max_length=18)
    is_active = models.BooleanField(default=True)
    date_joined = models.DateTimeField(default=timezone.now)

    type_user = models.CharField(
        'Тип', choices=Type.choices, default=Type.default, max_length=15)


And 2 proxy models:
class DefaultUser(User):

    objects = DefaultUserManager()

    def test(self):
        return 1

    class Meta:
        proxy = True

class VipUser(User):

    objects = VipUserManager()

    def test(self):
        return 2

    class Meta:
        proxy = True


Question: how can I get the result of user.test () in the template, depending on the type of the current user, without additional type checks?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2021-08-27
@pyHammer

Anton , you cannot get an inheriting class from an inherited one. You can access the test method only from instances of the DefaultUser or VipUser class on a non-User
PS
Where have you seen someone do this?

class User(AbstractBaseUser, PermissionsMixin):

    class Type(models.TextChoices):
        default = 'default', 'Стандартный Пользователь'
        vip = 'vip', 'Вип пользователь'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question