【问题标题】:adding / editing django generic foreign key objects添加/编辑 django 通用外键对象
【发布时间】:2015-03-12 05:56:33
【问题描述】:

模型设备:

class Device(models.Model):
    device_code = models.CharField(max_length=64,unique=True)
    is_enabled = models.BooleanField(default=False)

    def __unicode__(self):
        return u'%s: %s' % (self.device_code, 'ENABLED' if self.is_enabled else 'DISABLED')

模型属性值:

class AttributeValue(models.Model):
    attribute = models.ForeignKey(Attribute)
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')

    class Meta:
        abstract = True
        unique_together = (
            ('attribute', 'content_type','object_id'),
        )
        index_together = (
            ('content_type','object_id'),
        )
    @property        
    def formatted(self):
        """
        PLEASE SELECT RELATED ATTRIBUTE BEFORE USING THIS FUNCTION
        """
        return self.attribute.format % self.value

    def save(self,*args,**kwargs):
        if hasattr(self.content_object,'invalidate_cache') and callable(self.content_object.invalidate_cache):
            self.content_object.invalidate_cache()
        super(AttributeValue,self).save(*args, **kwargs)


    def __unicode__(self):
        return u'%s %s' % (self.attribute.name, self.value)

class NumericAttributeValue(AttributeValue):
    value = models.DecimalField(max_digits=12,decimal_places=4)


class LongTextAttributeValue(AttributeValue):
    value = models.TextField()

class ShortTextAttributeValue(AttributeValue):
    value = models.CharField(max_length=255)

class FileAttributeValue(AttributeValue):
    attribute_file = models.FileField(upload_to="attribute_imgs")

模型属性:

ATTRIBUTE_TYPE_CHOICES = (
    ('n','Numeric'),
    ('s','Short Text (255)'),
    ('m','Long Text')
)

class Attribute(models.Model):
        name = models.CharField(max_length=255)
        code = models.CharField(max_length=64,unique=True)
        attribute_type = models.CharField(max_length=1,choices=ATTRIBUTE_TYPE_CHOICES)
        sorting_order = models.PositiveIntegerField(default=0)
        show = models.BooleanField(default=False)
        format = models.CharField(max_length=64,default='%s')

        class Meta:
            ordering = ['sorting_order','name']

        def __unicode__(self):
            return self.name

在我的设备编辑(添加)页面中,它需要能够创建或选择一个属性,然后创建(或编辑/删除)一个属性值(可以是数值、长文本值、短文本值或与此属性关联的文件,以及当前(或新)设备。你会如何为这种场景创建一个 django formset?

【问题讨论】:

    标签: python django generic-foreign-key


    【解决方案1】:

    我必须解决类似的问题,django-polymorphic 为我工作。

    如果将抽象模型定义为父,那么它允许您选择父级基于Django Admin接口中的任何子模型(例如,在选择外键时)。

    您必须对模型和管理员进行一些更改才能使其正常工作(例如,您不需要 GenericForeignKey)。

    https://django-polymorphic.readthedocs.org/en/latest/

    【讨论】:

      猜你喜欢
      • 2015-03-20
      • 1970-01-01
      • 2012-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-20
      • 2021-04-04
      • 1970-01-01
      相关资源
      最近更新 更多