assign resulting csv file to django model

风流意气都作罢 提交于 2021-01-29 12:46:18

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/51765879/assign-resulting-csv-file-to-django-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!