Tuesday, August 30, 2011

Re: Unable To Delete Records

On Tuesday 30 August 2011,

Showket Bhat <scorpion.schizo@gmail.com> wrote:


> Hi All...


Hello Showket...


>

> I have created a small application where in i am inserting and

> deleting records. I am able to delete a single record however when a

> select all or more then one record it deletes the last record only.. I

> tried a lot but failed please help I am wring the Code and the Console

> output here....



Probably you should try using:


m = Medecine.objects.get(pk = i)


Instead of using:


m = Medecine.objects.get(id = i)


Also, try to handle the DoesNotExist and similar exception once you

try to retrieve the object using the /get()/ method. Also you can

reduce the look by using:


del_ids = request.POST.getlist('del_id')

Medecine.objects.filter(id__in = del_id).delete()


Which should be faster than your loop.


>

>

> ===========================================

> my function to delete a medecine from its table in views.py

> ===========================================

>

> def delete_medecine(request,medecine={}):

> print "------>>",request.POST

> list = []

> for i in request.POST['del_id']:

> m = Medecine.objects.get(id = i)

> m.delete()

> return HttpResponseRedirect('/medecine')

>

> ==============================================

> my template file

>

>

> <form action="/delete_medecine/" name = "medecines" method = "POST">

> <table cellspacing="0" cellpadding="0" border="0"><!-- Table -->

>

> <thead>

> <tr>

> <th><input type="checkbox" class="checkall" /></th>

> <th>SNo.</th>

> <th>Name</th>

> <th>Price</th>

> <th>Quantity</th>

> <th>Exp. Date</th>

> <th>Description</th>

> </tr>

> </thead>

>

> <tbody>

> {% for single in medecine_obj%}

> <tr>

> <td><input type="checkbox" value="{{single.id}}" name

> = "del_id" /></td>

> <td>{{single.id}}</td>

> <td>{{single.name}}</td>

> <td>{{single.price}}</td>

> <td>{{single.exp_date}}</td>

> <td>{{single.discription}}</td>

> <td><a href="#"><img src="/login/media/assets/

> action_check.png" alt="Check" /></a><a href="/delete_medecine/"><img

> src="/login/media/assets/action_delete.png" alt="Delete" /></a><a

> href="#"><img src="/login/media/assets/letter.png" alt="Letter" /></

> a></td>

> </tr>

> {%endfor%}

> </tbody>

> <a href="/medecine_form/"><input type = "button" name = "add"

> value = " Add Record " alt="Add" /></a>

> <input type = "submit" name = "delete" value = "Delete Records"

> alt="Delete" />

>

> </table>

> </form>

>

>

>

> ===========================

> My Console

> ===========================

>

> /usr/local/lib/python2.6/dist-packages/django/middleware/csrf.py:262:

> DeprecationWarning: CsrfResponseMiddleware and CsrfMiddleware are

> deprecated; use CsrfViewMiddleware and the template tag instead (see

> CSRF documentation).

> DeprecationWarning

> ================================== <QueryDict:

> {u'csrfmiddlewaretoken': [u'fea796e4bc7836bb8584140b71a0afcc'],

> u'del_id': [u'1', u'2', u'8'], u'delete': [u'Delete Records']}>

> ===== 8

> [30/Aug/2011 06:04:56] "POST /delete_medecine/ HTTP/1.1" 302 0

>

>

>

> ==============

>

>

> Please help


Best regards,

--

Daniel Molina Wegener <dmw [at] coder [dot] cl>

System Programmer & Web Developer

Phone: +56 (2) 979-0277 | Blog: http://coder.cl/

No comments:

Post a Comment