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()