[Fixed]-How do I edit and delete data in Django?


Say you have a model Employee. To edit an entry with primary key emp_id you do:

emp = Employee.objects.get(pk = emp_id)
emp.name = 'Somename'

to delete it just do:


so a full view would be:

def update(request, id):
   emp = Employee.objects.get(pk = id)
   #you can do this for as many fields as you like
   #here I asume you had a form with input like <input type="text" name="name"/>
   #so it's basically like that for all form fields
   emp.name = request.POST.get('name')
   return HttpResponse('updated')

def delete(request, id):
   emp = Employee.objects.get(pk = id)
   return HttpResponse('deleted')

In urls.py you’d need two entries like this:


I suggest you take a look at the docs



To do either of these you need to use something called queries.

check link below for really great documentation on that!

To Delete Data:

b = ModelName.objects.get(id = 1)

This will delete the Object of the model w/ an ID of 1

To edit Data:

b = ModelName.objects.get(id = 1)
b.name = 'Henry'

This will change the name of the Object of the model w/ an ID of 1 to be Henry


Read the following: The Django admin site. Then revise your question with specific details.


Leave a comment