I'm new to Django so any help would be appreciated.
Here's my situation.
I have a model Patient and I have classed based view for create, update, delete, details. They all are working perfectly. Now I have a dashboard where i am displaying patients details in datatable (like name, age, address etc), I also have a button which is supposed to add report for the patient (like lab tests- Urine test, Stool tests) etc. I successfully created create view but I am not being able to create update view. What I am looking for is when user clicks in report button in dashboard, it would be great if i could display create view if the report hasn't been added else display update view. I know in plain programming world, i could do database query and generate url accordingly but I am not sure how it's done in django.

CreateView is working. I don't how should i write create/update view for the dashboard url.
class Patient(models.Model):
invoice_num = models.CharField(max_length=15, unique=True)
patient_manual_id = models.IntegerField(unique=True)
name = models.CharField(max_length=100)
sex = models.CharField(max_length=1, choices=GENDER, default='M',)
class Meta:
ordering = ['id']
def get_absolute_url(self):
return reverse('patient_update', kwargs={'pk': self.pk})
@property
def patient_detail_link(self):
return "<a href='" + reverse("patient_update", kwargs={"pk": self.pk})+"'>Update</a>"
@property
def patient_id_link(self):
return "<a href='" + reverse("labreport_create", kwargs={"patient_id": self.pk})+"'>Report</a>"
def __str__(self):
return self.id
class Clia(models.Model):
patient = models.OneToOneField(
Patient,
on_delete=models.CASCADE,
primary_key=True,
)
clia_ft3 = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
clia_ft4 = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
clia_tsh = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
def __str__(self):
return self.id
def get_absolute_url(self):
return reverse('labreport_update', kwargs={'patient_id': self.pk})
views.py
class CliaCreate(SuccessMessageMixin, CreateView):
model = Clia
fields = ['clia_ft3', .... 'clia_fsh', 'clia_lh', 'clia_anti_tpo', 'clia_afp', 'clia_b_hcg', 'clia_cea', ]
#success_url = reverse_lazy('labreport_create')
success_message = "Record added successfully"
def dispatch(self, request, *args, **kwargs):
self.patient = get_object_or_404(Patient, pk=kwargs['patient_id'])
return super().dispatch(request, *args, **kwargs)
def form_valid(self, form):
form.instance.patient = self.patient
return super().form_valid(form)
class CliaUpdate(SuccessMessageMixin, UpdateView):
model = Clia
template_name='labreports/clia_form.html'
fields = ['clia_ft3', ..... 'clia_fsh', 'clia_lh', 'clia_anti_tpo', 'clia_afp', 'clia_b_hcg', 'clia_cea', ]
# success_url = reverse_lazy('labreport_update')
success_message = "Record updated successfully"
url.py
urlpatterns = [
path('<int:patient_id>', views.CliaCreate.as_view(), name='labreport_create'),
path('<int:patient_id>/update', views.CliaUpdate.as_view(), name='labreport_update'),
]