I am working on one python(django framework) project in which i have one user model which has 1000 users. I want to make one another column named priority id and need to put default id values in that priority id column accordingly (you can see it below).
id | username | priority_id | |
---|---|---|---|
1 | abc | [email protected] | 1 |
2 | xyz | [email protected] | 2 |
ofcourse i can do it manually using admin panel but for 1000 users it is time consuming.
how do i change models.py or admin.py or something else to achieve this?
CodePudding user response:
def save(self, *args, **kwargs):
self.priority_id = self.id
return super().save(*args, **kwargs)
CodePudding user response:
I don't know why you would ever want to do this, but you can achieve it by adding adding a BigAutoField to your user model. The field will then auto-increment the values for new users according to available ids.
In your models where you define the user, add following field.
priority_id = models.BigAutoField(null=False, unique=True)
CodePudding user response:
well, i tried it using shell and it worked:
p = Users.objects.all()
for x in p:
profile = Users.objects.get(pk=x.id)
profile.priority_id= x.id
profile.save()
CodePudding user response:
you can use this for me its working fine first you have to create with a default value and then use this function in views and just call once it will update the value as in id.
def update_value(request):
totaldata = modelsName.objects.all()
for j in totaldata:
modelsName.objects.filter(id=j.id).update(myid=j.id)
return HttpResponse('Success')