Skip to main content

A Django Field with extra contol over fields

Project description

django-ffield

django-ffield is a Django package that provides additional functionality and control over Django model fields, specifically focusing on file fields. this guide will you through the package's features, installation process, and provide examples of how to use it effectively.

Table of Contents


Installation

You can install django-ffield via pip:

pip  install  django-ffield

Usage

FileFField

FileFField extends Django's FileField and allows you to specify accepted file types and disallowed types during field initialization.

Example:

from django.db import models

from django_ffield.fields import FileFField

  

class  MyModel(models.Model):

image_field = FileFField(upload_to='images/', types=['image'])

pdf_field = FileFField(upload_to='pdfs/', types=['pdf'],
					disallowed_types=['image','video'])

video_field = FileFField(upload_to='videos/', types=['video'])

  

# Usage

my_instance = MyModel.objects.get(pk=1)

my_instance.image_field = 'example.png'  # Validates as it matches the 'image' type

my_instance.pdf_field = 'example.pdf'  # Validates as it matches the 'pdf' type

my_instance.video_field = 'example.mp4'  # Validates as it matches the 'video' type

FiledFField

FiledFField is a field that extends Django's FieldFile and provides additional properties to access file-related information.

Example:

from django.db import models

from django_ffield.fields import FiledFField

  

class  MyModel(models.Model):

file_field = FiledFField(upload_to='uploads/')

  

# Usage

my_instance = MyModel.objects.get(pk=1)

file = my_instance.file_field

  

# Accessing file properties

mime_type = file.mime_type # Returns the mimetype (e.g., 'image/png')

file_type = file.type # Returns the type of file (e.g., 'Image')

file_format = file.format # Returns the file format (e.g., 'png')

Utility Functions

django-ffield provides a utility function for detecting file MIME types.

This function detects the MIME type of a file by reading the first 2048 bytes

note : the file should be opened in binary mode.

Example:

from django_ffield.utils import meme_type

from django.core.files import File

  

with  open('example.png', 'rb') as  file:

type, subtype = meme_type(file)

print(f"Type: {type}, Subtype: {subtype}")

Validators

django-ffield includes a custom file type validator that can be used with FileFField.

This validator ensures that the uploaded file's type is among the specified types.

Example:

from django_ffield.validators import FileTypeValidator

  

class  MyModel(models.Model):

file_field = models.FileField(upload_to='uploads/', validators=[

		FileTypeValidator(
		allowd_types=['image', 'pdf', 'mp4'],
		disallowed_types = ["audio"],
		)

])

  

# Usage

my_instance = MyModel.objects.get(pk=1)

my_instance.file_field = 'example.mp3'  # This will raise a ValidationError since 'mp3' is not in the accepted types

Contributing

Feel free to contribute to django-ffield.

License

This project is licensed under the MIT License - see the LICENSE file for details.


Feel free to explore and utilize django-ffield to enhance your Django projects. If you have any questions, issues, or feature requests, please open an issue on our GitHub repository. Happy coding!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-ffield-0.0.9.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

django_ffield-0.0.9-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file django-ffield-0.0.9.tar.gz.

File metadata

  • Download URL: django-ffield-0.0.9.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for django-ffield-0.0.9.tar.gz
Algorithm Hash digest
SHA256 d9dd9bb45644de0a738db933661c06da3e72040249f4ff6f9c6993918f5348e6
MD5 6503d493f604ed9a0db77f35eec979b1
BLAKE2b-256 523889b6d379d4b8f0e1e52cba109fd94797ca490ad89c577b899787d500c255

See more details on using hashes here.

File details

Details for the file django_ffield-0.0.9-py3-none-any.whl.

File metadata

File hashes

Hashes for django_ffield-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 525df862c0961c7a740454ea3f449d16e402d3d6cd0e43860f81d0813de94f38
MD5 261a1f21ba80993a6edfded882f235c3
BLAKE2b-256 d9aabd8064ecd1a8eba4a98cf6ff3166e89052f5705f86a58507376a9eeec20f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page