In Django project I have error NoReverseMatch after form save, redirecting to "parent view"

models.py

This is a models code - personnel

class Personnel(models.Model):
    ID_Personnel = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    NameFirst = models.CharField(max_length=50)
    NameLast = models.CharField(max_length=50)
    IsActive = models.BooleanField(default=True, editable=True)
    TimeCreation = models.DateTimeField(default=datetime.now, editable=False)

class IdentityDocument(models.Model):
    ID_IdentityDocument = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    ID_Personnel = models.ForeignKey(Personnel, on_delete=models.PROTECT)
    NumberSerial = models.CharField(max_length=50)
    DateIssue = models.DateField(null=True)
    DateExpiration = models.DateField(null=True)
    NameIssuer = models.CharField(max_length=100, null=True)
    IsActive = models.BooleanField(default=True, editable=True)
    TimeCreation = models.DateTimeField(default=datetime.now, editable=False)

views.py - personnel

def personnel_edit(request, pk):
    person = get_object_or_404(Personnel, ID_Personnel=pk)
    if request.method == 'POST':
        form = PersonnelForm(request.POST, instance=person)
        if form.is_valid():
            person = form.save(commit=False)
            person.save()
            return redirect('personnel_detail', pk=person.ID_Personnel)
    else:
        form = PersonnelForm(instance=person)
    return render(request, 'personnel/personnel_edit.html', {'form': form})

def personnel_detail(request, pk):
    person = get_object_or_404(Personnel, ID_Personnel=pk)
    try:
        document = IdentityDocument.objects.filter(ID_Personnel=pk).order_by('ID_IdType', '-DateExpiration').distinct(
            'ID_IdType')
    except IdentityDocument.DoesNotExist:
        document = None
    return render(request, 'personnel/personnel_detail.html',
                  {'person': person, 'document': document})

views.py - Setting ID_Personnel in Form

def identitydocument_new(request, fkey):
    if request.method == 'POST':
        form = IdentityDocumentForm(request.POST)
        if form.is_valid():
            iddoc = form.save(commit=False)
            iddoc.ID_Personnel = Personnel.objects.get(ID_Personnel=fkey)
            iddoc.save()
            return redirect('personnel_detail', pk=fkey)
    else:
        form = IdentityDocumentForm()
    return render(request, 'personnel/identitydocument_edit.html', {'form': form})
def identitydocument_edit(request, pk):
    doc = get_object_or_404(IdentityDocument, ID_IdentityDocument=pk)
    if request.method == 'POST':
        form = IdentityDocumentForm(request.POST, instance=doc)
        if form.is_valid():
            doc = form.save(commit=False)
            doc.save()
            return redirect('personnel_detail', pk=doc.ID_Personnel)
    else:
        form = IdentityDocumentForm(instance=doc)
    return render(request, 'personnel/identitydocument_edit.html', {'form': form})

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.personnel_list, name='personnel_list'),
    path('personnel/<uuid:pk>/', views.personnel_detail, name='personnel_detail'),
    path('personnel/new', views.personnel_new, name='personnel_new'),
    path('personnel/<uuid:pk>/edit/', views.personnel_edit, name='personnel_edit'),
    path('personnel/identitydoc/<uuid:fkey>/', views.identitydocument_new, name='identitydocument_new'),
    path('personnel/identitydoc/<uuid:pk>/edit/', views.identitydocument_edit, name='identitydocument_edit'),
]

Form creates new documents and saveing it fine, but after editing it brings NoReverseMatch error.

Traceback

2 Answers

0
Daniel Roseman On Best Solutions

As that error shows, ID_Personnel is a reference to the entire related Personnel document. The URL needs the ID of that object. So you should do:

return redirect('personnel_detail', pk=doc.ID_Personnel_id)

But fundamentally this is a sign that your field name is incorrect. As I say, that is a reference to the object, not an ID, so it shouldn't have a name that implies it's an ID. Just call it Personnel. (Also, Python style is to use lower_case_with_underscore for attributes, so really it should be just personnel.)

0
Oleksii Dubniak On

Try this

from django.urls import reverse

redirect(reverse('personnel_detail', kwargs={'pk': doc.ID_Personnel}))