'Django - export one txt file per row to local drive

I'm stuck with this as I can't find any relevant tutorials online. I would like to

  1. export all of the data in a model to the txt file format {actually xml but I think that's besides the point}
  2. where each row in the model is a seperate file
  3. and saved to a relative local path to the database
  4. with the folder and filenames for each being derived from two fields in the model
  5. I also need these to replace any existing files of the same name

Where would I start? All I can do currently is export a txt file with all records together as a downloadable attachment.

This is what the code would like if I were presenting it as a HTML page, however of course the filename and folder fields are not referenced.

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<movie>
  <plot />
  <outline />
  <title>{{ misc.title }}</title>
  <release date>{{ misc.d_released }}</release date>

  </fileinfo>
</movie>

Here are the fields

class misc(models.Model):
d_created = models.DateTimeField(auto_now_add=True)
filename = models.CharField(max_length=250)
folder = models.ForeignKey(folder, default="1", blank=True, null=True, on_delete=models.CASCADE)
title = models.CharField(max_length=250, default='', blank=True, null=True)
d_released = models.CharField(max_length=25, default='', blank=True, null=True)
description = models.TextField(blank = True, null = True)

def __str__(self):
    return self.title

class Meta:
    ordering = ('-id', )


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source