About the auto-increase PK

18 views
Skip to first unread message

Holland_zwz

unread,
Aug 8, 2015, 6:10:35 AM8/8/15
to Django users
Hi all,
 I want to add one auto-increase PK which start from one given value(such as start from 1000).

I try to write the model as following:

class MyModel(models.Model):
    mid = models.AutoField(verbose_name='My ID',primary_key=True,default='1000')


But only one record can be add when i add records in admin web site.

Any ways to support this? thanks

Regards,
Hollandz


Markus Holtermann

unread,
Aug 8, 2015, 7:03:52 AM8/8/15
to Django users
Hi Hollandz,

You can't do that from within Django automatically. You'd need to write your own database migration to update the database as the assignment of the primary key value is part of the database and not Django. Have a look at https://docs.djangoproject.com/en/1.8/ref/migration-operations/#django.db.migrations.operations.RunSQL

/Markus

ADEWALE ADISA

unread,
Aug 8, 2015, 7:24:31 AM8/8/15
to django...@googlegroups.com

Am not 100% sure about this, but what about creating a normal Integer field, then override the save method of the model to auto-increment the value starting from 1000 ? So that any new record added, its value will be 1000+ total record +1

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a66c5ec3-d0e8-41c8-98df-ef2c6f6c7126%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages