I am building a django app for pathology/laboratory reporting system. I need a form that will take value for different tests like (HIV-negative, some test-10). Since there are 100 and 1000 of tests and these test are grouped by categories like chemical test, microscopic test. I need to render a template with a form for these tests. I used django-mptt and building a structure in django admin seems easy but i couldn't make it work in frontend templates. Below are my project codes and descriptions.
class TestCategory(MPTTModel):
name = models.CharField(max_length=100)
parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')
is_active = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now_add=True)
update_at = models.DateTimeField(auto_now=True)
class Meta:
verbose_name_plural = "Test Categories"
class MPTTMeta:
order_insertion_by = ['name']
def __str__(self):
return self.name
class Test(models.Model):
QUALITATIVE = "1"
QUANTITATIVE = "0"
TEST_TYPE_CHOICES = [
(QUALITATIVE, 'QUALITATIVE'),
(QUANTITATIVE, 'QUANTITATIVE'),
]
test_category = TreeForeignKey(TestCategory, on_delete=models.CASCADE, related_name='test_category')
name = models.CharField(max_length=50)
unit = models.CharField(max_length=10)
specimen = models.ForeignKey(Specimen, on_delete=models.CASCADE, blank=True, null=True)
test_type = models.CharField(max_length=2, choices=TEST_TYPE_CHOICES, default=QUALITATIVE)
reference_text = models.TextField()
price = models.DecimalField(max_digits=10, decimal_places=2, blank=True, default=0, verbose_name='Price Per Unit')
is_active = models.BooleanField(default=True)
def __str__(self):
return self.name
class Meta:
ordering = ['test_category']
# views.py
def report_create_view(request, pk):
context ={}
form = CreateReportForm(request.POST or None)
if form.is_valid():
patient = self.objects.all().filter(patient_id=pk)
form.instance.patient_id = patient.patient_id
form.save()
context['form']= form
context['patient'] = Patient.objects.all().filter(pk=pk)
context['nodes'] = TestCategory.objects.all()
return render(request, "lab/report_form.html", context)
#template report_form.html
{% load crispy_forms_tags %}
{% load mptt_tags %}
<form method="post" novalidate>
{% csrf_token %}
{{ form|crispy }}
{% for node,structure in nodes|tree_info %}
{% if structure.new_level %}<ul><li>{% else %}</li><li>{% endif %}
{{ node.name }}
{% for level in structure.closed_levels %}</li></ul>{% endfor %}
{% endfor %}
<button type="submit" class="btn btn-success">Add Report</button>
</form>
This is what my template looks right now. As you can see my form has only categories name (I am getting TestCategory model only node the actual Test model. How can actually render tests grouped by category?
cat
-category model --test model ...and other test checkbox based on Test.test_category=TestCategory.id **if there are sub category**cat
sub cat
– Pushpan Feb 28 '21 at 12:40