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: > > On 23 Jun 2020, at 23:32, Oleg Kishenkov wrote: > > > 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) > > Just to clarify this, are you indicating that when you create the > record you generate a unique custom key yourself and insert this? > > > -- Clive > -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/53a1e6a8-b449-474c-8c33-b49e4c17a84fn%40googlegroups.com.