0

Django 2.2.3

如何将参数传输到抽象模型的 Meta 内部类?

我的努力:

class GeneralUtmAbstract(models.Model):
    where = ""

    class Meta:
        def __init__(self):
            self.verbose_name_plural = "General UTM-labels: {}".format(GeneralUtmAbstract.where)
            self.verbose_name = verbose_name_plural

class GeneralUtm(GeneralUtmAbstract):
    where = "Both"

class BingUtm(GeneralUtmAbstract):
    where = "Bing"

class GoogleUtm(GeneralUtmAbstract):
    where = "Google"

我的代码没有引发任何错误。但是冗长的名称并没有按计划显示。我得到“通用 utm”而不是“通用 UTM 标签:两者”。

4

1 回答 1

0

您需要使模型抽象,然后决定哪一行将where显示为表的名称(为什么要这样做,我无法理解):

class GeneralUtmAbstract(models.Model):
    where = ""

    class Meta:
        abstract = True
        verbose_name_plural = "General UTM-labels" + str(GeneralUtmAbstract.objects.filter(where=something)

如果想法是在同一模型/表中逻辑隔离对象,请考虑使用代理模型;看这里

于 2019-08-01T07:18:30.297 回答