import os.path from reportlab.pdfgen import canvas from django.db import models from django.conf import settings class Entry(models.Model): doc = models.TextField() file = models.FilePathField( path=os.path.join(settings.MEDIA_URL, '/files'), match='*\.pdf',) def save(self, *args, **kwargs): """Writing entry content to new pdf file, saving it in file system and make record to database.""" counter = 0 def make_path(counter): """Check existing and make new file.""" path = os.path.join( settings.STATICFILES_DIRS[1], 'files', 'f' + str(counter) + '.pdf') if os.path.isfile(path): counter += 1 return make_path(counter) return path curr_path = make_path(counter) pdf_file = canvas.Canvas(curr_path) pdf_file.drawString(100, 750, self.doc) pdf_file.save() self.file = curr_path super(Entry, self).save(*args, **kwargs)