python - Using Django ----If there are more than 1 record then displaying it and if there is only 1 record then i am deleting it -


what problem code displaying records whether there 1 record or many...but want delete if there 1 record.... advise code writing obliged....

here view

def delete(request): form = searchform(request.post)

searched_data = information.objects.filter(name="full_name").count()  d_data = none  if form.is_valid():     if (searched_data == 1):         d_data = information.objects.filter(name= form.cleaned_data.get('full_name')).delete()     else:         d_data = information.objects.filter(name=form.cleaned_data.get('full_name'))   context = {     'form': form,     'd_data': d_data,  }  return render(request, 'delete.html', context) 

i not unless form valid:

if form.is_valid():     d_data = information.objects.filter(name=form.cleaned_data.get('full_name'))      if len(d_data) == 1:         d_data.delete()         d_data = none else:     d_data = none  context = {     'form': form,     'd_data': d_data, }  return render(request, 'delete.html', context) 

note: set d_data none after it's been deleted.

on wheter use d_data.count() or len(d_data), please see this


Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -