MultiUpload Django Admin

2

I have a django project in which from the admin I can upload photos one at a time, now, I want to upload more than one for the same element (database registration) and I would like to know what is the way less traumatic and easy, since it is usually just add enctype="multipart / form-data" but in django everything is very far fetched.

I have found django-admin-multiupload but I find it somewhat complex for the simple thing I need, it is not an external form but to make the simple ImageField field allow multiple files , you have to do some way of configuring that, right?

link

models.py

class Photo(models.Model):
    pho_code = models.AutoField(db_column='PHO_code', primary_key=True) 
    pho_url = models.ImageField(db_column='PHO_url', upload_to='media/documents/%Y/%m/%d') 
    poi = models.ForeignKey(Poi, models.DO_NOTHING, db_column='POI_code')

    class Meta:
        managed = False
        db_table = 'PHOTO'

    def __unicode__(self):
        return str(self.pho_url)

admin.py

class PhotoAdmin(admin.ModelAdmin):
    list_display = ['pho_code', 'pho_url', 'poi']
    form = PhotoForm

    class Meta:
        model = Photo

    
asked by RuralGalaxy 26.09.2016 в 12:51
source

1 answer

1

With your current model you can not do what you want. However you have two options:

  • Use a field JSON - The fields type JSON have a free format, so you could use it to build a dictionary with n number of images. For the use of JSONField you have several options and packages that you can find on the Internet. One of these options is offered by Django's own project: JSONField .

  • Use an ArrayField field - Like the previous one, it allows you to manage field collections. Unlike the previous option, all fields __ must be of the same type, for example the field ImageField . Like the previous one, the field ArrayField only works in PostgreSQL.

  • Use a related table - That is, you make a table of images that relates each image to your Photo model using foreign keys, ForeignKey . You can see usage examples in the Django documentation .

  • answered by 27.09.2016 в 15:50