MySQL Django Primary Key Update

Sorry for my bad english, my problem is:

I am trying to update PK in Django with the .save () method, but when I save the Django object, duplicate the object with the same data but differetn PK, for example:

from gestion_empleados.Models import Empleados
>>> e = Empleados.objects.get(pk="56789034U")
>>> e.pk
u'56789034U'
>>> e.pk = "11111111L"
>>> e.save()
>>> e.pk
'11111111L'
>>> e2 = Empleados.objects.get(pk="56789034U")
>>> e2
<Empleados: Juan 56789034U>
>>> e
<Empleados: Juan 11111111L>

The objects are the same with different PKs, and I want to change the PK without duplicating the object.

Any solution? Thank!

+4
source share
3 answers

I do not think that Django allows you to change the primary key of an object. You may need to delete the original object.

e2.delete()

According to Django docs

. , .

Django Docs

+6

Django Model.save() , PK, , INSERT UPDATE.

: PK SQL, , , (ok, still , , ), PK - . : ( SQL , ).

+2

, "11111111L" . , - :

  

e3 = Empleados.objects.get(pk = "11111111L" )

  

And then make sure that e3 contains. Once you confirm that it is, you can simply use the following statement to get rid of the object with the primary key "56789034U" (provided that you save e2):

  

e2.delete ()

  
0
source

All Articles