When setting up the structure of my database, I was so stupid that I set the Student_id field to primary_key = True for the Student class. I realized much later that there are (rare) cases when it is necessary to change what was said by student_id. When you do this through a form, Django automatically duplicates the student, which I donβt want.
I would like to change "primary_key = True" to "unique = True" and I am wondering how to do this.
My current plan is to add a field named "id" to the Student class, apply the migrations and go to the shell and just assign it a number with a for loop:
counter = 0 for s in Student.objects.all(): counter += 1 s.id = counter s.save()
Then I will go back to my models.py and change the line "primary_key = True" to "unique = True". How can I make sure that Django processes the "id" field, as it would with classes without a primary key (ie automatically assign a new identifier when a new student is added to the database)?
source share