Having more than one PDF field on a table in Django

database-design, django, pdf

Solution

If you need to have more than one pdf per manual, use a one-to-many relationship as a ForeignKey in another table. There's nothing wrong with having multiple models.

class Manual(models.Model):
    brand = models.CharField(max_length=255)
    model = models.CharField(max_length=255)

class ManualPDF(models.Model):
    manual = models.ForeignKey(Manual)
    pdf = models.FileField(upload_to='pdf')

In your view code (or form or model code) you can then get all the PDFs for a manual using `_set` which will return a QuerySet of the ManualPDF model objects:

some_manual = Manual.objects.get(id=1)
some_manual_pdfs = some_manual.manualpdf_set.all()

There more info in the official Django docs:

https://docs.djangoproject.com/en/dev/topics/db/examples/many_to_one/

Problem

I am making a Manual admin as part of a big project. Each manual has a brand, a model and has at least one PDF. from django.db import models ``` class Manual(models.Model): brand = models.CharField(max_length=255) model = models.CharField(max_length=255) manual = models.ImageField(upload_to='pdf') ``` Two questions: - How can I model a PDFField, or a generic field, rather than an Image field? - Is it possible for the manual field to have more than one file without having to make another table? Thanks!

Original source