# Path: db_file_storage/model_utils.py
# def delete_file(instance, filefield_name):
#     """Delete the file (if any) from the database.
# 
#     Call this function immediately AFTER deleting the instance.
#     """
#     file_instance = getattr(instance, filefield_name)
#     if file_instance:
#         DatabaseFileStorage().delete(file_instance.name)
# 
# def delete_file_if_needed(instance, filefield_name):
#     """Delete file from database only if needed.
# 
#     When editing and the filefield is a new file,
#     deletes the previous file (if any) from the database.
#     Call this function immediately BEFORE saving the instance.
#     """
#     if instance.pk:
#         model_class = type(instance)
# 
#         # Check if there is a file for the instance in the database
#         if model_class.objects.filter(pk=instance.pk).exclude(
#             **{'%s__isnull' % filefield_name: True}
#         ).exclude(
#             **{'%s__exact' % filefield_name: ''}
#         ).exists():
#             old_file = getattr(
#                 model_class.objects.only(filefield_name).get(pk=instance.pk),
#                 filefield_name
#             )
#         else:
#             old_file = None
# 
#         # If there is a file, delete it if needed
#         if old_file:
#             # When editing and NOT changing the file,
#             #   old_file.name == getattr(instance, filefield_name)
#             #   returns True. In this case, the file must NOT be deleted.
#             # If the file IS being changed, the comparison returns False.
#             #   In this case, the old file MUST be deleted.
#             if (old_file.name == getattr(instance, filefield_name)) is False:
#                 DatabaseFileStorage().delete(old_file.name)
# 
# Path: db_file_storage/compat.py



from django.db import models
from db_file_storage.model_utils import delete_file, delete_file_if_needed
from db_file_storage.compat import reverse
    book_pages_pk = models.AutoField(primary_key=True)
    bytes = models.TextField()
    filename = models.CharField(max_length=255)
    mimetype = models.CharField(max_length=50)


class BookCover(models.Model):
    book_cover_pk = models.AutoField(primary_key=True)
    bytes = models.BinaryField()
    filename = models.CharField(max_length=255)
    mimetype = models.CharField(max_length=50)


class Book(models.Model):
    book_pk = models.AutoField(primary_key=True)
    name = models.CharField(max_length=100, unique=True)
    index = models.FileField(
        upload_to='model_filefields_example.BookIndex/bytes/filename/mimetype',
        blank=True, null=True
    )
    pages = models.FileField(
        upload_to='model_filefields_example.BookPages/bytes/filename/mimetype',
        blank=True, null=True
    )
    cover = models.ImageField(
        upload_to='model_filefields_example.BookCover/bytes/filename/mimetype',
        blank=True, null=True
    )

    def get_absolute_url(self):

# Path: db_file_storage/model_utils.py
# def delete_file(instance, filefield_name):
#     """Delete the file (if any) from the database.
# 
#     Call this function immediately AFTER deleting the instance.
#     """
#     file_instance = getattr(instance, filefield_name)
#     if file_instance:
#         DatabaseFileStorage().delete(file_instance.name)
# 
# def delete_file_if_needed(instance, filefield_name):
#     """Delete file from database only if needed.
# 
#     When editing and the filefield is a new file,
#     deletes the previous file (if any) from the database.
#     Call this function immediately BEFORE saving the instance.
#     """
#     if instance.pk:
#         model_class = type(instance)
# 
#         # Check if there is a file for the instance in the database
#         if model_class.objects.filter(pk=instance.pk).exclude(
#             **{'%s__isnull' % filefield_name: True}
#         ).exclude(
#             **{'%s__exact' % filefield_name: ''}
#         ).exists():
#             old_file = getattr(
#                 model_class.objects.only(filefield_name).get(pk=instance.pk),
#                 filefield_name
#             )
#         else:
#             old_file = None
# 
#         # If there is a file, delete it if needed
#         if old_file:
#             # When editing and NOT changing the file,
#             #   old_file.name == getattr(instance, filefield_name)
#             #   returns True. In this case, the file must NOT be deleted.
#             # If the file IS being changed, the comparison returns False.
#             #   In this case, the old file MUST be deleted.
#             if (old_file.name == getattr(instance, filefield_name)) is False:
#                 DatabaseFileStorage().delete(old_file.name)
# 
# Path: db_file_storage/compat.py



# Path: demo_and_tests/model_filefields_example/models.py
from django.db import models
from db_file_storage.model_utils import delete_file, delete_file_if_needed
from db_file_storage.compat import reverse

    book_pages_pk = models.AutoField(primary_key=True)
    bytes = models.TextField()
    filename = models.CharField(max_length=255)
    mimetype = models.CharField(max_length=50)


class BookCover(models.Model):
    book_cover_pk = models.AutoField(primary_key=True)
    bytes = models.BinaryField()
    filename = models.CharField(max_length=255)
    mimetype = models.CharField(max_length=50)


class Book(models.Model):
    book_pk = models.AutoField(primary_key=True)
    name = models.CharField(max_length=100, unique=True)
    index = models.FileField(
        upload_to='model_filefields_example.BookIndex/bytes/filename/mimetype',
        blank=True, null=True
    )
    pages = models.FileField(
        upload_to='model_filefields_example.BookPages/bytes/filename/mimetype',
        blank=True, null=True
    )
    cover = models.ImageField(
        upload_to='model_filefields_example.BookCover/bytes/filename/mimetype',
        blank=True, null=True
    )

    def get_absolute_url(self):

        return reverse('model_files:book.edit', kwargs={'pk': self.pk})