Django Rest Framework为不同的帐户层提供不同的图像大小3内置和可能性

nxagd54h  于 2023-03-09  发布在  Go
关注(0)|答案(2)|浏览(115)
#a link to a thumbnail that's 200px in height - Basic
#a link to a thumbnail that's 400px in height - Basic and Intermediate
#a link to the originally uploaded image - Basic, Intermediate and Premium
class Profile(models.Model):

        MEMBERSHIP = (
            ('BASIC', 'Basic'),
            ('PREMIUM', 'Premium'),
            ('ENTERPRISE', 'Enterprise')
        )

        user = models.OneToOneField(User, on_delete=models.CASCADE)
        membership = models.CharField(max_length=10, choices=MEMBERSHIP, default='BASIC')

        def __str__(self):
            return f'{self.user.username} {self.membership} Profile'

唯一的方法,我知道如何做3内置在层是像上面。
我不知道如何做更多的不同图像大小的会员资格,可以从管理面板添加。我想有他们作为一个模型,并根据需要添加一个新的用户。

xdyibdwo

xdyibdwo1#

你可以创建一个名为Membership的新模型:

class Membership(models.Model):
  name = models.CharField(max_length=10, default='BASIC')
  thumbnail_max_width = models.IntgerField()
  thumbnail_max_height = models.IntgerField()

 class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    membership = models.ForigenKey(Membership)

    def __str__(self):
        return f'{self.user.username} {self.membership} Profile'

现在你可以从管理面板创建所有你想要的成员类型

unguejic

unguejic2#

Gio欢迎来到stackoverflow!实际上,为了让用户能够为不同的付费级别提供不同的图像大小,你可以添加两种图像大小,一种是基本的,一种是高级的

class Profile(models.Model):

    MEMBERSHIP = (
        ('BASIC', 'Basic'),
        ('PREMIUM', 'Premium'),
        ('ENTERPRISE', 'Enterprise')
    )

    user = models.OneToOneField(User, on_delete=models.CASCADE)
    membership = models.CharField(max_length=10, choices=MEMBERSHIP, default='BASIC')
    basic_thumbnail_height = models.PositiveIntegerField(default=200)
    premium_thumbnail_height = models.PositiveIntegerField(default=400)

    def __str__(self):
        return f'{self.user.username} {self.membership} Profile'

    def thumbnail_link(self):
        if self.membership == 'BASIC':
            return f'https://something.com/thumbnail/{self.user.id}/{self.basic_thumbnail_height}'
        elif self.membership == 'PREMIUM':
            return f'https://something.com/thumbnail/{self.user.id}/{self.premium_thumbnail_height}'
        else:
            return f'https://something.com/original/{self.user.id}'

相关问题