vikrantCool vikrantCool - 5 months ago 170
Python Question

How to update an object of model in django using update_or_create

I want to update an object of a model using update_or_create method.But when i use this another object is created.
my models.py

class SubmitHobby(models.Model):
M_id=models.IntegerField(default=0)
name=models.CharField(max_length=255)
mob=models.IntegerField(default=0,blank=True,null=True)
is_approve=models.BooleanField(default=False)


i want to update is_approve field on the basis of M_id (M_id=234)

def approve(request):

context = RequestContext(request)
name= = request.GET.get('name')
mid=request.GET.get('M_Id')

obj=SubmitHobby.objects.update_or_create(M_id=mid)
obj.is_approve=True
obj.save()

Answer

You just need to get the object you need,set value you need to change and save it,the object gets updated.

obj = modelname.objects.get(pk=<id>)
obj.field=<value>
obj.save()
Comments