将结果csv文件分配给Django模型 [英] assign resulting csv file to django model

查看:49
本文介绍了将结果csv文件分配给Django模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个由不同歌曲组成的网站.这些歌曲包含不同的属性,其中之一是我通过django的fileField字段上传的midi文件.当我使用表格添加具有这些属性的歌曲时,我会调用一个脚本,该脚本生成带有midi属性信息的csv文件.

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.

问题是我想将此生成的csv文件直接分配给另一个Filefield,即,当我创建表单时,刚提交表单后,此csv就分配给了fileField.

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.

这是位于forms.py的代码.通过这种形式,我添加了具有以下属性的新歌曲.

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代码.此视图用于调用将添加新歌曲的表单.

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)

我的models.py代码.我需要添加的唯一模型字段是csv文件.提交表单后,我想自动添加它.

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

推荐答案

假设您拥有这样的模型,

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


更新

from django.core.files import File

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

这篇关于将结果csv文件分配给Django模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆