Home > Enterprise >  how to list sub fields of model fields in Django ListView?
how to list sub fields of model fields in Django ListView?

Time:09-21

Under my model work_allocation I have a field called activity_name. Fort all activity_name there are sub fields associated as shown

here

In the views.py I have written a simple class based view to list the activity_task(get_task_list), which is working fine. But I am not able to see the associated sub fields.

Not sure what modification am I supposed to make for this to happen. Below are my project details:

models.py

from django.db import models


class Status_Code(models.Model):
    Types = models.CharField(max_length=50, blank=True)

    def __str__(self):
        return self.Types


class Assignee_Name(models.Model):
    emp_name = models.CharField(max_length=50, blank=True)

    def __str__(self):
        return self.emp_name


class Priority(models.Model):
    priority = models.CharField(max_length=50, blank=True)

    def __str__(self):
        return self.priority


class work_allocation(models.Model):
    activity_name = models.CharField(max_length=100)
    JIRA_tkt = models.CharField(max_length=100)
    Assignee_name = models.ForeignKey(Assignee_Name, on_delete=models.CASCADE)
    Status_code = models.ForeignKey(Status_Code, on_delete=models.CASCADE)
    Description = models.TextField()
    Planned_start_date = models.DateField(auto_now_add=True)
    Planned_end_date = models.DateField(auto_now_add=True)
    actual_start_date = models.DateField(auto_now_add=True)
    actual_end_date = models.DateField(auto_now_add=True)
    priority = models.ForeignKey(Priority,on_delete=models.CASCADE)

    def __str__(self):
        return self.activity_name

views.py

def createTask(request):
    if request.method == 'POST':
        form = Work_alloc_Form(request.POST)
        if form.is_valid():
            data_to_db = work_allocation()
            data_to_db.activity_name = form.cleaned_data['activity_name']
            data_to_db.JIRA_tkt = form.cleaned_data['JIRA_tkt']
            data_to_db.Assignee_name = form.cleaned_data['Assignee_name']
            data_to_db.Status_code = form.cleaned_data['Status_code']
            data_to_db.Planned_start_date = form.cleaned_data['Planned_start_date']
            data_to_db.Planned_end_date = form.cleaned_data['Planned_end_date']
            data_to_db.actual_start_date = form.cleaned_data['actual_start_date']
            data_to_db.actual_end_date = form.cleaned_data['actual_end_date']
            data_to_db.Description = form.cleaned_data['Description']
            data_to_db.priority = form.cleaned_data['priority']
            data_to_db.save()
            messages.success(request, f'Profile has been updated successfully')
            return redirect('/home')
        else:
            messages.error(request, AttributeError)
    else:
        form = Work_alloc_Form()
    return render(request, 'tasks/create_work_allocation.html', context={'form': form})


class get_task_list(ListView):
    model = work_allocation

For example, on the web page with the GET view I should see "new task" and its associated fields like JIRA, assignee name, status code etc.

I hope I am making myself clear with my question.

Please suggest

CodePudding user response:

Since you already defined the __str__ for each class, all we need to do is just create a template HTML for the ListView and display each field accordingly.

Let's say we have 2 records:

>>> from my_app.models import *
>>> for item in work_allocation.objects.values():
...     print(item)
... 
{'id': 1, 'activity_name': 'new task', 'JIRA_tkt': '1234', 'Assignee_name_id': 1, 'Status_code_id': 1, 'Description': 'qwerty', 'Planned_start_date': datetime.date(2021, 9, 20), 'Planned_end_date': datetime.date(2021, 9, 20), 'actual_start_date': datetime.date(2021, 9, 20), 'actual_end_date': datetime.date(2021, 9, 20), 'priority_id': 1}
{'id': 2, 'activity_name': 'another task', 'JIRA_tkt': '56', 'Assignee_name_id': 2, 'Status_code_id': 2, 'Description': 'asd', 'Planned_start_date': datetime.date(2021, 9, 20), 'Planned_end_date': datetime.date(2021, 9, 20), 'actual_start_date': datetime.date(2021, 9, 20), 'actual_end_date': datetime.date(2021, 9, 20), 'priority_id': 2}

Now, create a file my_app/templates/my_app/work_allocation_list.html

{% for item in object_list %}
    <hr/>
    {{ item.activity_name }}<br/>
    {{ item.JIRA_tkt }}<br/>
    {{ item.Assignee_name }}<br/>
    {{ item.Status_code }}<br/>
{% endfor %}
<hr/>

ListView will automatically use it to render the list of work_allocation objects:

enter image description here

  • Related