1

I have a django model (say model1) where I have my own primary key, but now I want to change the primary key to default id field. But the problem if I try to change is, I have another model (say model2) which has many to many dependency to the model1 and it is causing errors when I am trying to modify the field.

Is there any way I can change the primary key of this model?

ex:

class model2(models.Model):
    id = ..
    ...
    model1 = models.ManyToManyField("model1", blank=True)

classs model1(models.Model):
     Name = models.charField(primary_key=True, max_length=280)
     .. 

Now I want to change the primary key of model1 back to Id.

cezar
  • 11,616
  • 6
  • 48
  • 84
  • If I try to edit it by removing primary key and then running the migrations, it asks me to provide a default for id, if i give some random default such as timezone.now, I get the following error. Error: TypeError: int() argument must be a string, a bytes-like object or a number, not 'datetime.datetime' ? – Tarun Golthi Oct 12 '17 at 17:47
  • Did you check this question: https://stackoverflow.com/questions/2055784/what-is-the-best-approach-to-change-primary-keys-in-an-existing-django-app?rq=1 ? – cezar Oct 12 '17 at 20:08
  • I checked they were using older version of django and were using south, I am using django version 1.11. so it didn't help much. – Tarun Golthi Oct 12 '17 at 22:12
  • You are using a datetime object as a default. Convert that to a string. – Michael Josephson Oct 13 '17 at 12:22
  • @MichaelJosephson that won't help a lot, as the id should be integer. – cezar Oct 13 '17 at 12:32

0 Answers0