Django - 多列主键
我想在 django 中实现多列主键。
我尝试实现一个 AutoSlugField() 来连接我的列值(外键/日期)...
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= slug 的 True
参数...但它创建了带有“-1”“-2”的新实例...并且根本没有进行任何更新...
我错过了什么吗?
谢谢,
尤安
I would like to implement multicolumns primary keys in django.
I've tried to implement an AutoSlugField() which concatenate my columns values(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))
When I pass the following parameters :
- '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 ????
I tried to add primary_key=True
parameter to the slug... but it creates new instance with a "-1" "-2" ... and NO update is made at all...
Did I miss something ?
Thanks,
Yoan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我使用的 autoslugfield 的解释。
http://packages.python.org/django-autoslug/fields.html
问候,
尤安
Here is the explanation of the autoslugfield I used.
http://packages.python.org/django-autoslug/fields.html
Regards,
Yoan