0

I'm developing a website that consists of different songs. These songs contain different attributes, one of them is a midi file that I upload through the fileField field of django. When I add a song with these attributes using a form, I call a script that generates a csv file with midi attribute information.

The problem is that I would like to assign this resulting csv file directly to another Filefield, i.e. when I create the form, this csv is assigned to a fileField, just after submitting the form.

I'd like to know if somebody could please help me with this. If you need any code or something else let me know.

Here's my code at forms.py. With this form, I add a new song with the following atributes.

class FormCancion(forms.ModelForm):
class Meta:
    model = Cancion
    fields= ['titulo','creacion','midi','dificultad','nota_pad_verde','nota_pad_gris','nota_pad_azul','nota_pad_amarillo','nota_pad_rojo']

views.py code. This view is used to call the form that will add the new song.

def crearCancion(request):

cancion=Cancion()
if request.method=="POST":
    formulario=FormCancion(request.POST,request.FILES,instance=cancion)
    if formulario.is_valid():
        formulario.save()
        subprocess.call(['python', '/home/josema/MEGA/Universidad/Universidad/PROYECTO/MIDIPIRCUSSION/MIDIPIRCUSSION_APP/static/MIDIPIRCUSSION_APP/parser.py', '/home/josema/MEGA/Universidad/Universidad/PROYECTO/MIDIPIRCUSSION/media/'+str(cancion.midi)])
        return redirect('/ListadoCanciones/')
else:
    formulario=FormCancion()
    context={'formulario':formulario}
    return render(request,"nuevaCancion.html",context)

My models.py code. The only model field that I need to add is the csv file. I'd like to add it automatically after I submit the form.

class Cancion(models.Model):

    titulo=models.CharField(max_length=60)
    creacion=models.DateField(default=timezone.now)

    avanzado="Avanzado"
    intermedio="Intermedio"
    principiante="Principiante"
    dificultades=((avanzado, 'Avanzado'), (intermedio, 'Intermedio'), (principiante, 'Principiante'))
    dificultad=models.CharField(max_length=15, choices=dificultades)

    @property
    def filename(self):
        return self.midi.path
    midi = models.FileField()

    @property
    def filename(self):
        return self.csv.path
    csv = models.FileField()
Josema_23
  • 306
  • 1
  • 2
  • 13

1 Answers1

2

Assuming you have a model like this,

class SampleModel(models.Model):

    doc = models.FileField(upload_to='media/', null=True, blank=True)


then do something like this to add file to model,

from django.core.files import File

myfile = open("sample.csv", 'r')
sample = SampleModel.objects.create(doc=File(myfile))


UPDATE

from django.core.files import File

myfile = open("sample.csv", 'r')
sample = Cancion.objects.create(csv=File(myfile))
JPG
  • 82,442
  • 19
  • 127
  • 206
  • That's more less what I want, but It doesnt work at all. I only want to add one file, only the fileField atribute of the model – Josema_23 Aug 09 '18 at 19:02
  • Yeah... Updated the answer.. Please hqve a look – JPG Aug 10 '18 at 01:37
  • I think it is not exactly want I want. This adds a new model with all the atributes. Most of the atributes are added by a form, but after I submit the form is when I need to assign the file to the model field. I updated the post with my code. – Josema_23 Aug 10 '18 at 09:49
  • @Josema_23 could you fix the indentations ? – JPG Aug 10 '18 at 09:52
  • How do you get the data required to create the csv? – JPG Aug 10 '18 at 10:00
  • I have a parser.py file, that receives a midi file, extracts some information, creates the csv file and saves the information in the csv file. This parser.py file is called in the preceding view. – Josema_23 Aug 10 '18 at 10:06
  • Then whats the problem ? I don't understand :( – JPG Aug 10 '18 at 10:10
  • I used your solution, and it adds a new model with the atributes that I add in SampleModel.objects.create(doc=File(myfile)). What I need is to add this csv file to the model that is created by the form. Sorry if I don't explain it really well :( – Josema_23 Aug 10 '18 at 10:16
  • I don't mean you create a **new model**. I used a sentence as ***Assuming you have a model like this***. – JPG Aug 10 '18 at 10:17
  • 1
    Thanks! Your solution works. It was my problem, I was not doing it in the correct place of the code. – Josema_23 Aug 10 '18 at 10:20