35๐
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'
emp.save()
to delete it just do:
emp.delete()
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')
emp.save()
return HttpResponse('updated')
def delete(request, id):
emp = Employee.objects.get(pk = id)
emp.delete()
return HttpResponse('deleted')
In urls.py youโd need two entries like this:
(r'^delete/(\d+)/$','myproject.myapp.views.delete'),
(r'^update/(\d+)/$','myproject.myapp.views.update'),
I suggest you take a look at the docs
๐คVasil
3๐
To do either of these you need to use something called queries.
check link below for really great documentation on that!
(https://docs.djangoproject.com/en/2.2/topics/db/queries/)
To Delete Data:
b = ModelName.objects.get(id = 1)
b.delete()
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'
b.save()
This will change the name of the Object of the model w/ an ID of 1 to be Henry
๐คReal.Cryptc
- How do I update an already existing row when using ModelForms?
- Django Whitenoise 500 server error in non debug mode
- Saving form data rewrites the same row
- Django and Long Polling
- Rendering individual fields in template in a custom form
-9๐
Read the following: The Django admin site. Then revise your question with specific details.
๐คS.Lott
- Fabric โ sudo -u
- Cannot connect to localhost API from Android app
- Using .extra() on fields created by .annotate() in Django
- Display a boolean model field in a django form as a radio button rather than the default Checkbox
Source:stackexchange.com