Skip to main content

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!

Metadata

Release files for django-ffield 0.0.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-ffield 0.0.9
File Size Uploaded Provenance Actions
django-ffield-0.0.9.tar.gz 4.7 kB Without provenance Details for django-ffield-0.0.9.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-ffield 0.0.9
File Interpreter ABI Platform Actions
django_ffield-0.0.9-py3-none-any.whl Python 3 none any Details for django_ffield-0.0.9-py3-none-any.whl

Total release size: 10.4 kB

Release files / django-ffield-0.0.9.tar.gz

Download URL django-ffield-0.0.9.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
d9dd9bb45644de0a738db933661c06da3e72040249f4ff6f9c6993918f5348e6
BLAKE2b-256 checksum
How to use checksums
523889b6d379d4b8f0e1e52cba109fd94797ca490ad89c577b899787d500c255
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release files / django_ffield-0.0.9-py3-none-any.whl

Download URL django_ffield-0.0.9-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
525df862c0961c7a740454ea3f449d16e402d3d6cd0e43860f81d0813de94f38
BLAKE2b-256 checksum
How to use checksums
d9aabd8064ecd1a8eba4a98cf6ff3166e89052f5705f86a58507376a9eeec20f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Security for django-ffield 0.0.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page