Custom Primary Key

119 views
Skip to first unread message

Soumen Khatua

unread,
Jun 23, 2020, 3:19:20 PM6/23/20
to django...@googlegroups.com
Hi Folks,

In Django is there any way to create custom Primark Key like: M01234GH, M056YUOIP, etc.

Please help me guys.

Thank you in advance

Regards,
Soumen Khatua

David Chorpash

unread,
Jun 23, 2020, 4:07:02 PM6/23/20
to django...@googlegroups.com
Hi Soumen,

Are you looking for this?

You should be able to create a field in your model and set the primary_key to True


--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPUw6WbZ_nrXgk%3DufWfq39%2BZJmN-nmF0DzeuFK9tqgFW-Os3vQ%40mail.gmail.com.

Soumen Khatua

unread,
Jun 23, 2020, 4:09:52 PM6/23/20
to django...@googlegroups.com
In the documentation they are specified, How to set Primary Key in Django model but In my case, I want to generate custom Primary Key?!

Oleg Kishenkov

unread,
Jun 23, 2020, 6:33:56 PM6/23/20
to Django users
Hello Soumen, you should use a CharField with the primary_key=True attribute for your model. This way no no automatic primary key field is generated, your field will be implicitly unique and non-null though. Only one primary key is allowed in a model.

class Foo(models.Model)
    id = models.CharField(max_length=15, primary_key=True)

Regards,
Oleg

Clive Bruton

unread,
Jun 23, 2020, 9:49:45 PM6/23/20
to django...@googlegroups.com
Just to clarify this, are you indicating that when you create the
record you generate a unique custom key yourself and insert this?


-- Clive

Soumen Khatua

unread,
Jun 24, 2020, 1:25:22 AM6/24/20
to django...@googlegroups.com
Yes, at the time of add a new record in model, automatically I want to create a custom ID and insert it into model

--
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.

Clive Bruton

unread,
Jun 24, 2020, 10:43:44 AM6/24/20
to django...@googlegroups.com

On 24 Jun 2020, at 06:24, Soumen Khatua wrote:

> Yes, at the time of add a new record in model, automatically I want
> to create a custom ID and insert it into model

There is a post about this at Stack Overflow:

https://stackoverflow.com/questions/52070462/django-generate-custom-id


-- Clive

Oleg Kishenkov

unread,
Jun 24, 2020, 12:21:02 PM6/24/20
to Django users
Yes, first you generate your custom key, second you specify it as the id of an object.
id = 'A1B2C3D4E5'
foo = Foo(id=id)
foo.save()
This code would create a record in the database. I also have to note that if you read this record to an object, changed it and saved it, the record wouldn't change, another record would be created with the new id
foo = Foo.objects.filter(id=id)[0]
foo.id = 'X1Y2Z3'
foo.save()
Foo.objects.all()
<QuerySet [<Foo: Foo object (A1B2C3D4E5)>, <Foo: Foo object (X1Y2Z3)>]>

Oleg

среда, 24 июня 2020 г. в 04:49:45 UTC+3, Clive Bruton:
Reply all
Reply to author
Forward
0 new messages