Django - 多列主键

时间:2010-09-10 10:54:40

标签: python django django-models foreign-keys composite-primary-key

我想在django中实现多列主键。

我试图实现一个AutoSlugField(),它连接我的列值(foreignkey / dates)......

models.py:

class ProductProduction(models.Model):
    enterprise = models.ForeignKey('Enterprise')
    product = models.ForeignKey('Product')
    date = models.DateTimeField()
    count = models.IntegerField()
    slug = AutoSlugField(populate_from=
    lambda instance: instance.enterprise.username + '-' + instance.product.name + '-' + str(date))

当我传递以下参数时:

 - 'Megacorp','robot','09/10/2010',5 => slug = 'Megacorp-robot-09/10/2010'
... the next time in pass the triplet, a new value has been inserted :
 - 'Megacorp','robot','09/10/2010',10 => slug = 'Megacorp-robot-09/10/2010' 
        => same slug value => insert ????

我尝试将primary_key=True参数添加到slug中......但它创建了一个带有“-1”“-2”的新实例......并且根本没有更新...

我错过了什么吗?

谢谢,

Yoan

1 个答案:

答案 0 :(得分:0)

以下是我使用的autoslugfield的解释。

http://packages.python.org/django-autoslug/fields.html

此致

Yoan