tags:

views:

1343

answers:

3

I am using django 1.0 and I have created my models using the example in the Django book. I am able to perform the basic function of adding data; now I need a way of retrieving that data, loading it into a form (change_form?! or something), EDIT it and save it back to the DB. Secondly how do I DELETE the data that's in the DB? i.e. search, select and then delete!

Please show me an example of the code I need to write on my view.py and urls.py for perform this task.

+2  A: 

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
A: 

Vasil,

How do you deal with editing of many fields?

How do you retrive the data to an editing form?

Gath
Please edit you original question next time you want to ask something further. And you'll need to do a bit more exploration in the docs and the book to get into it. Waiting for answers on questions like this won't get you far.
Vasil
Then, after editing your question to include this revision, please delete this non-answer.
S.Lott
A: 

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

S.Lott
this topic doesn't have anything to do with admin site. he is asking for model instance method (delete).
israkir
@israkir: The built-in Django admin site will allow a person to delete model instances and edit data. That's what it does. The topic says "loading it into a form (change_form?! or something), EDIT it and save it back to the DB." That's what the admin site does.
S.Lott