Home > Back-end >  when i update/edit data is duplicated instead of being updated in django
when i update/edit data is duplicated instead of being updated in django

Time:02-04

data is duplicated instead of being updated in django, please help me to overcome this i also tried update method but issues i have faced is image not displayed, therefore i is used save method which will save and make copy of anthor object which i dont want. it should update the same object.

views.py

def EuAdmin(request, pk):
        pi = EmailDb.objects.get(id=pk)
        if request.method == 'POST':
            institution = request.POST.get('institution', '')
            fullname = request.POST.get('fullname', '')
            email = request.POST.get('email', '')
            contact = request.POST.get('contact', '')
            position = request.POST.get('position', '')
            uploadd = request.FILES.get('upload', '')
            sdata = EmailDb(institution=institution, fullname=fullname, contact=contact, email=email, position=position, uploaddata=uploadd)
            sdata.save()
            return HttpResponseRedirect("/eadmin")
        return render(request, 'NEC/eupdate.html', {'pi': pi})

models.py

class EmailDb(models.Model):
    institution = models.CharField(max_length=300, blank=True, null=True)
    fullname = models.CharField(max_length=50, blank=True, null=True)
    contact = models.IntegerField()
    email = models.CharField(max_length=300, blank=True, null=True)
    position = models.CharField(max_length=100, blank=True, null=True)
    uploaddata = models.FileField(upload_to='appointment_letter')

    def __str__(self):
        return self.fullname

CodePudding user response:

That's because you create a new EmailDb object. You can edit the one with:

from django.shortcuts import get_object_or_404


def EuAdmin(request, pk):
    pi = get_object_or_404(EmailDb, pk=pk)
    if request.method == 'POST':
        institution = request.POST.get('institution', '')
        fullname = request.POST.get('fullname', '')
        email = request.POST.get('email', '')
        contact = request.POST.get('contact', '')
        position = request.POST.get('position', '')
        uploaded = request.FILES.get('upload', '')
        pi.institution = institution
        pi.fullname = fullname
        pi.contact = contact
        pi.email = email
        pi.position = position
        pi.uploaddata = uploaded
        pi.save()
        return HttpResponseRedirect('/eadmin')
    return render(request, 'NEC/eupdate.html', {'pi': pi})

Note: It is better to use a Form [Django-doc] than to perform manual validation and cleaning of the data. A Form will not only simplify rendering a form in HTML, but it also makes it more convenient to validate the input, and clean the data to a more convenient type.


Note: Models normally have no Db suffix. A model is not a table, it is stored in a relational database as a table, but even then it has extra logic like validators, managers, etc.


Note: Please use an EmailField [Django-doc] instead of a CharField [Django-doc] to store an email address, this can do validation to check if the entered data is indeed an email address.


Note: It is often better to use get_object_or_404(…) [Django-doc], then to use .get(…) [Django-doc] directly. In case the object does not exists, for example because the user altered the URL themselves, the get_object_or_404(…) will result in returning a HTTP 404 Not Found response, whereas using .get(…) will result in a HTTP 500 Server Error.

CodePudding user response:

sdata = EmailDb(institution=institution, fullname=fullname, contact=contact, email=email, position=position, uploaddata=uploadd)
sdata.save()

This performs an INSERT SQL statement.

Since you already have object just set the value and call save() as

def EuAdmin(request, pk):
    pi = EmailDb.objects.get(id=pk)
    if request.method == 'POST':
        institution = request.POST.get('institution', '')
        fullname = request.POST.get('fullname', '')
        email = request.POST.get('email', '')
        contact = request.POST.get('contact', '')
        position = request.POST.get('position', '')
        uploaded = request.FILES.get('upload', '')
        pi.institution = institution
        pi.fullname = fullname
        pi.contact = contact
        pi.email = email
        pi.position = position
        pi.uploaddata = uploaded
        pi.save()
        return HttpResponseRedirect("/eadmin")
    return render(request, 'NEC/eupdate.html', {'pi': pi})
  • Related