[Answered ]-Django: Customizing ManyToManyField form options

2๐Ÿ‘

โœ…

So, is there any way to customize each of the list items so that I can add divs to the HTML and display other bits of data for each object like you would do in a for loop?

Assuming your view is sending a context variable of all the Contacts like the following.

def myview(request, *a, **kw):
    # querying the related objects
    company_model = CompanyModel.objects.get(id=some_id)
    # creating a dictionary containing the queryset that we want based on these related objects.
    data['contacts'] = Contacts.objects.filter(parent__id=company_model.id)
    # You can still pass your form in with this data like so
    data['form'] = MyForm
    # passing that queryset and the form to the template to be displayed.
    return render(request, "path/to/template", data)

You should be able to iterate over that queryset in your template like so.

# this would go inside your form
{% for contact in contacts %}
    <div>{{ contact.cl_name }}</div>
    <div>{{ contact.cl_slug }}</div> cl_dt_created
    <div>{{ contact.cl_dt_created }}</div>
    <input type="checkbox" value="{{ contact.id }}" name="contact" />
{% endfor %}
๐Ÿ‘คmarcusshep

Leave a comment