django

时间:2016-06-15 07:30:23

标签: django

我在我的项目中使用django(1.9)-python(2.7),我的模型是使用inspectdb从遗留数据库(Oracle)创建的。这里的问题是遗留数据库具有复合主键。

我对问题的解决方法是使用复合字段http://django-composite-field.readthedocs.io/en/latest/

from composite_field import CompositeField

class Thing(models.Model):
    first = models.CharField(max_length=32)
    second = models.IntegerField()
    both = CompositeField(('first', 'second'))

但我最终遇到以下错误 ...

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "<console>", line 4, in Thing
  File "C:\Python27\Lib\site-packages\composite_field\base.py", line 70, in __in
it__
    self.subfields = deepcopy(self.subfields)
AttributeError: 'CompositeField' object has no attribute 'subfields'

提前致谢!!

1 个答案:

答案 0 :(得分:0)

使用here中的django-composite-field

示例用法如下:

class CoordField(CompositeField):
    x = models.FloatField()
    y = models.FloatField()

class Place(models.Model):
    name = models.CharField(max_length=10)
    coord = CoordField()

p = Place(name='Foo', coord_x=42, coord_y=0)
q = Place(name='Foo', coord=p.coord)
q.coord.y = 42

(从官方文档中复制)