Django OneToOne关系。分类数据

时间:2018-07-19 10:28:32

标签: database relationship models

在开发Django项目时,我的模型存在以下问题。

型号:

class AttributeType(models.Model):
    uuid = models.UUIDField(verbose_name='uuid', primary_key=True, editable=False, default=uuid4)
    caption = models.CharField(max_length=255, unique=True, verbose_name='Название')

    def __str__(self):
        return self.caption

    class Meta:
        verbose_name = 'Тип атрибута'
        verbose_name_plural = 'Типы атрибута'


class AttributeAbstract(models.Model):
    uuid = models.UUIDField(verbose_name='uuid', primary_key=True, editable=False, default=uuid4)
    caption = models.CharField(max_length=255, verbose_name='Название')
    attributetype = models.ForeignKey(to=AttributeType, on_delete=models.CASCADE, verbose_name='Тип атрибута')

    def __str__(self):
        return self.caption

    class Meta:
        unique_together = ('caption', 'attributetype', )
        verbose_name = 'Атрибут'
        verbose_name_plural = 'Атрибуты'
        abstract = True


class AttributeList(AttributeAbstract):

    class Meta:
        verbose_name = 'Атрибут типа "Список"'
        verbose_name_plural = 'Атрибуты типа "Список"'


class AttributeField(AttributeAbstract):

    class Meta:
        verbose_name = 'Атрибут типа "Поле"'
        verbose_name_plural = 'Атрибуты типа "Поле"'

class ObjectTypeAttribute(models.Model):
    uuid = models.UUIDField(verbose_name='uuid', primary_key=True, editable=False, default=uuid4)
    attribute = models.ForeignKey(to=AttributeAbstract, on_delete=models.CASCADE, verbose_name='Атрибут')
    position = models.IntegerField()

问题是,如果我想根据 AttributeType 创建 ObjectTypeAttribute 的实例,该如何处理一对一关系?换句话说,如何考虑 AttributeType ObjectTypeAttribute 实例中定义外键?

非常感谢您的协助!

0 个答案:

没有答案