Search code examples
djangomany-to-manydjango-ormdjango-generic-views

How to make ManyToMany field's value to show up in Django generic DeleteView


I have been trying to send model objects (as it is done for Django ListViews) in conjunction with Django generic DeleteView. Frankly I am not sure if I can do what I am trying to do. Given below is what I am using and to some extent it is working:

Models

class ApsProcesses(models.Model):
    aps_process = models.CharField(primary_key=True, max_length=1, ..)
    aps_process_text = models.CharField(max_length=35, verbose_name='Process Desc')

class ProcessStatuses(models.Model):
    proc_status = models.CharField(primary_key=True, max_length=1, ...)
    proc_status_text = models.CharField(max_length=55, verbose_name='Short Desc')
    applicable_process_compo = models.ManyToManyField(ApsProcesses, related_name='proc_choices', ..)
    proc_status_weightage = models.PositiveSmallIntegerField(null=True, ..)

Views

class ProcStatusesDeleteView(DeleteView):
    template_name = ..
    model = ProcessStatuses
    context_object_name = 'proc_statuses'

In my listview page I have the named url on the objects and on click it takes to the user to the delete page where I have the underlying data displayed (for the model instance selected by the user) and then two options are provided to the user - Confirm delete or Cancel and go back to the calling page. Here is what I am doing in the template:

Template

... some codes truncated here
<table>
    <tr>
        <th>Status Cd</th>
        <th>Status </th>
        <th>Process </th> 
        <th>Weightage </th>
    </tr>

<tr>
    <td style='color:red'> <strong>{{ proc_statuses }}</strong></td>
    <td>{{ proc_statuses.proc_status_text }}</td>
    <td>{{ proc_statuses.applicable_process_compo__aps_process }}</td> {# .aps_process_text #} {# << Tried to get field's attributes using both, not working though #}
    <td>{{ proc_statuses.proc_status_weightage }}</td>
</tr>
</table>
<p>Are you sure you want to delete <a style='color:red'><strong> the record?</strong></a> </p>

<input type="submit" class="btn btn-info" value="Confirm"/> 
<a href="{% url 'proc_statuses_list' %}" class="btn btn-info" role="button">Cancel</a>

... some codes truncated here

Using the above template code all the field values are displayed, excepting the reference to the ManyToManyField applicable_process_compo. If I use the following (i.e. without a double underscore or a dot with the ManyToManyField):

<td>{{ proc_statuses.applicable_process_compo }}

I am getting <app_name>.ApsProcesses.None in the column for the field aps_process. So I know that the particular model ApsProcesses is being accessed, but the value saved in the particular model instance is not showing up.

My question is: How can I get the value/s of the ManyToManyField to show up in the page?


Solution

  • This is what I did to resolve the issue:

    Modified the model ProcessStatuses as shown below:

    class ProcessStatuses(models.Model):
        proc_status = models.CharField(primary_key=True, max_length=1, ...)
        proc_status_text = models.CharField(max_length=55, verbose_name='Short Desc')
        applicable_process_compo = models.ManyToManyField(ApsProcesses, related_name='proc_choices', ..)
        proc_status_weightage = models.PositiveSmallIntegerField(null=True, ..)
    
        def get_processes(self):
            if self.applicable_process_compo:
                return '%s' % " / ".join([applicable_process_compo.aps_process for applicable_process_compo in self.applicable_process_compo.all()])
    

    And in the template modified the line for accessing the ManyToMany field's values like this:

    <td>{{ proc_statuses.get_processes }}</td>
    

    Now I am getting the values to show up on the page as desired.