0

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. enter image description here

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'),

]

7
  • Can you share your models please?
    – ruddra
    Mar 18, 2019 at 6:37
  • Is there any relation between report and patient?
    – ruddra
    Mar 18, 2019 at 6:53
  • 'class Patient(models.Model):' 'invoice_num = models.CharField(max_length=15, unique=True)' 'patient_manual_id = models.IntegerField(unique=True)' 'class Clia(models.Model):' 'patient = models.OneToOneField(' 'Patient, on_delete=models.CASCADE, primary_key=True,)'
    – Pushpan
    Mar 18, 2019 at 6:57
  • 1
    instead of comments, please update the actual question :) .
    – ruddra
    Mar 18, 2019 at 7:00
  • Please check my updated answer. hope it helps. thanks
    – ruddra
    Mar 18, 2019 at 7:03

2 Answers 2

0

Updated Answer:

Add a new method in model:

class Patient(models.Model):
    ...
    def create_report(self):
        return reverse("labreport_create", kwargs={"patient_id": self.pk})

I would recommend something like this:

You can create a separate UpdateView same as create view:

class CliaUpdate(SuccessMessageMixin, UpdateView):
    model = Clia
    fields = ['clia_ft3', ...[other fields]..., 'clia_cea', ]
    #success_url = reverse_lazy('labreport_create')
    success_message = "Record updated successfully"

Now in dashboard template, you can add the following logic:

{% for patient in object_list %} // rendering all patient
    # do rest of stuff for rendering dashboard
    {% if patient.clia %}
        <a href=<a href="{{ patient.clia.get_absolute_url }}">Report</a>
    {% else %}
        <a href="{{ patient.create_report }}">Report</a>
    {% endif %}

{% endfor %}

Explanation:

In template, I am checking a patient has any Clia, as they have OneToOne relation, so I can use patient.clia to check it exists.

4
  • I am generating url from model def get_absolute_url(self): return reverse('patient_update', kwargs={'pk': self.pk})
    – Pushpan
    Mar 18, 2019 at 7:10
  • I am not sure that would be relevant. BTW, I am using: docs.djangoproject.com/en/2.1/ref/templates/builtins/#url
    – ruddra
    Mar 18, 2019 at 7:16
  • my dashboard doesn't have have any django templating codes..just a plain table html and everything is passed through datatable-django-rest-framework...I have to do if/else (to generate create/update url) in models i think... @property def patient_id_link(self): (something here)
    – Pushpan
    Mar 19, 2019 at 5:31
  • TBH, I don't like putting html code in python. I have updated the answer according to your comments, except for the html parts.
    – ruddra
    Mar 19, 2019 at 6:01
0

Adding slug to View worked

slug_field = 'patient_id'
slug_url_kwarg = 'patient_id'

I also had to change the url

url(r'^create/(?P<patient_id>\d+)', views.CliaCreate.as_view(), name='labreport_create'),
url(r'^update/(?P<patient_id>\d+)', views.CliaUpdate.as_view(), name='labreport_update'),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.