Is there a more wieldy way to create a tsv/csv row template?

I was just editing a tsv row template for a tsv download format we provide. The template contains a single 3068 character line, which is really hard to edit:

{% load customtags %}{% get_many_related_rec row.labels row.peak_group_label as label_recs %}{{ row.msrun_sample.sample.name }}	{{ row.msrun_sample.sample.tissue.name }}	{{ row.msrun_sample.sample.time_collected|durationToMins }}	{{ row.name }}	{% for mcpd in row.compounds.all %}{% if not forloop.first %};{% endif%}{{ mcpd.name }}{% endfor %}	{% for mcpd in row.compounds.all %}{% if not forloop.first %};{% endif%}{% get_case_insensitive_synonyms mcpd.synonyms as inssyns %}{% for mcpdsyn in inssyns %}{% if not forloop.first %}/{% endif%}{{ mcpdsyn }}{% endfor %}{% endfor %}	{{ row.formula }}	{% for label_rec in label_recs %}{% if not forloop.first %},{% endif %}{{ label_rec.element }}{% endfor %}	{% for msrsrec in row.msrun_sample.sample.msrun_samples.all %}{% if msrsrec.ms_data_file %}{% if mzxml_exists %}, {% endif %}{% define True as mzxml_exists %}{{ msrsrec.ms_data_file.filename }}{% endif %}{% endfor %}{% if pg.msrun_sample.sample.msrun_samples.count == 0 or pg.msrun_sample.sample.msrun_samples.count == 1 and pg.msrun_sample.sample.msrun_samples.first.ms_data_file is None %}None{% endif %}	{{ row.total_abundance }}	{% for label_rec in label_recs %}{% if not forloop.first %},{% endif %}{{ label_rec.enrichment_fraction }}{% endfor %}	{% for label_rec in label_recs %}{% if not forloop.first %},{% endif %}{{ label_rec.enrichment_abundance }}{% endfor %}	{% for label_rec in label_recs %}{% if not forloop.first %},{% endif %}{% if label_rec.normalized_labeling is None %}None{% else %}{{ label_rec.normalized_labeling }}{% endif %}{% endfor %}	{{ row.peak_annotation_file.filename }}	{{ row.msrun_sample.sample.animal.name }}	{{ row.msrun_sample.sample.animal.genotype }}	{{ row.msrun_sample.sample.animal.body_weight }}	{{ row.msrun_sample.sample.animal.age|durationToWeeks }}	{{ row.msrun_sample.sample.animal.sex }}	{{ row.msrun_sample.sample.animal.diet }}	{{ row.msrun_sample.sample.animal.feeding_status }}	{% if row.msrun_sample.sample.animal.treatment is None %}None{% else %}{{ row.msrun_sample.sample.animal.treatment.name }}{% endif %}	{% if row.msrun_sample.sample.animal.infusate.short_name is None %}None{% else %}{{ row.msrun_sample.sample.animal.infusate.short_name }}{% endif %}	{% for link in row.msrun_sample.sample.animal.infusate.tracer_links.all %}{% if not forloop.first %};{% endif%}{% if link.tracer.name is None %}None{% else %}{{ link.tracer.name }}{% endif %}{% endfor %}	{% for link in row.msrun_sample.sample.animal.infusate.tracer_links.all %}{% if not forloop.first %};{% endif%}{% if link.tracer.compound.name is None %}None{% else %}{{ link.tracer.compound.name }}{% endif %}{% endfor %}	{% for link in row.msrun_sample.sample.animal.infusate.tracer_links.all %}{% if not forloop.first %},{% endif%}{% if link.concentration is None %}None{% else %}{{ link.concentration }}{% endif %}{% endfor %}	{{ row.msrun_sample.sample.animal.infusion_rate }}	{% for study in row.msrun_sample.sample.animal.studies.all %}{% if not forloop.first %}, {% endif%}{{ study.name }}{% endfor %}

It’s used as the rowtmplt in this generator:

def tsv_template_iterator(rowtmplt, headtmplt, res, qry, dt):
    yield headtmplt.render({"qry": qry, "dt": dt})
    for row in res:
        yield rowtmplt.render({"qry": qry, "row": row})

Is there any way I can have a template with hard returns in it that aren’t rendered, just to make it easier to deal with?

You should not use django templates for this. There’s a csv library in Python that will do this stuff and do it correctly. The code you have above seems unlikely to work with some simple cases like study.name containing a comma for example.

Yeah, a comma would thwart parsing. Good point.

I kind of expected the response might be to use something other than a template. I’ll google for some examples (though google did get me into this predicament in the first place, lol).

I had a “D’oh” moment at some point since my last response. All I need is a method that turns a record (from a queryset) into a list that I can give to csv.