Django Multiple File Field

前端 未结 4 1771
遇见更好的自我
遇见更好的自我 2020-12-30 00:24

Is there a model field that can handle multiple files or multiple images for django? Or is it better to make a ManyToManyField to a separate model containing Images or File

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-30 00:53

    FilerFileField and FilerImageField in one model:

    They are subclasses of django.db.models.ForeignKey, so the same rules apply. The only difference is, that there is no need to declare what model we are referencing (it is always filer.models.File for the FilerFileField and filer.models.Image for the FilerImageField).

    Simple example models.py:

    from django.db import models
    from filer.fields.image import FilerImageField
    from filer.fields.file import FilerFileField
    
    class Company(models.Model):
        name = models.CharField(max_length=255)
        logo = FilerImageField(null=True, blank=True)
        disclaimer = FilerFileField(null=True, blank=True)
    

    Multiple image file fields on the same model in models.py:

    Note: related_name attribute required, it is just like defining a foreign key relationship.

    from django.db import models
    from filer.fields.image import FilerImageField
    
    class Book(models.Model):
        title = models.CharField(max_length=255)
        cover = FilerImageField(related_name="book_covers")
        back = FilerImageField(related_name="book_backs")
    

    This answer code taken from django-filer document

提交回复
热议问题