Save file to directory, from the view - Django

0

I have an application with which I upload files, and subsequently those files are consulted; the files have the following nomenclature: EXAMPLE_7777_AAAA.pdf

If the file belongs to group 7777, the file is saved in the path: upload_to = 'Files / Group / 7777'

If the file belongs to group 7746, it is saved in the path: upload_to = 'Files / Group / 7746'

However, there are 31 groups, so I have to create 31 tables or models for to be able to save in each table the files belonging to each group.

The point is that I want to validate the group and from the group save the file in its specific path, with this the files will be stored in a single table.

That's how I do it

if grupo=='7777':
    datos=ArchivosGrupo7777() # Nombre de la tabla actual
    datos.usuario=usuarioSesion
    datos.save()

if grupo=='7746':
    datos=ArchivosGrupo7746() # Nombre de la tabla actual
    datos.usuario=usuarioSesion
    datos.save()

However the idea is that there are not too many tables and that it is something like this:

if grupo=='7777':
    datos=Archivos()/*Nombre de la tabla
    datos.usuario=usuarioSesion
    datos.save(directorio='Archivos/Grupo/7777')

if grupo=='7746':
    datos=Archivos()/*Nombre de la tabla
    datos.usuario=usuarioSesion
    datos.save(directorio='Archivos/Grupo/7746')


Modelos-Ejemplo

class ArchivosGrupo7777(models.Model):
    fecha = models.DateField(auto_now_add=True)
    proyecto=models.CharField(default='Proyecto 7777',max_length=30)
    ruta = models.FileField(upload_to='Archivos/Grupo/7777')
    usuario=models.ForeignKey(User)

class ArchivosGrupo7746(models.Model):
    fecha = models.DateField(auto_now_add=True)
    proyecto=models.CharField(default='Proyecto 7746',max_length=30)
    ruta = models.FileField(upload_to='Archivos/Grupo/7746')
    usuario=models.ForeignKey(User)
    
asked by Noel L 14.08.2018 в 21:08
source

1 answer

1

I think you do not need to have a model for each group of files, you can do it dynamically, having the grupo as a parameter something like this:

class Archivos(modesl.Model):

    grupo = models.CharField(max_length=3)
    fecha = models.DateField(auto_now_add=True)
    proyecto=models.CharField(default='Proyecto 7777',max_length=30)
    ruta = models.FileField(upload_to='Archivos/Grupo/7777')
    usuario=models.ForeignKey(User)

# en la view
grupo = '777' # asumiendo que el grupo es valido
Archivos.create(
        grupo=grupo,
        proyecto='Proyecto %s' % (grupo),
        ruta='Archivos/Grupo/%s' %(grupo),
        usuario=usuarioSesion
        )

This way you will have all the files in a single table, and adding the group field you can do group by to get all the same group

Now if you want to have a table for each group, I do not really see how that can be practical, but you can use a Generic ForeignKey, as such way that you would have a table file with a "dynamic" ForeignKey which would be the group, I leave a link to the documentation:

link

    
answered by 15.08.2018 в 04:49