[Fixed]-Using Django β€”-If there are more than 1 record then displaying it and if there is only 1 record then i am deleting it

1πŸ‘

βœ…

I would not do anything unless the form is 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: I set d_data to None after it’s been deleted.

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

πŸ‘€Pynchia

Leave a comment