简体   繁体   中英

Django - Multiple columns primary key

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

Here is the explanation of the autoslugfield I used.

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

Regards,

Yoan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM