Skip to main content

It is a Python package that allows users to validate a post value against the choices defined in a Django model choice field and returns the corresponding choice data for database storage.

Project description

validate_choice_field

Introduction

validate_choice_field package provides a simple and efficient way to validate user-submitted data against predefined choices in a Django model choice field. It ensures that the posted data matches one of the available choices and returns the corresponding value required for database storage. If no match is found, it returns None.

How It Works

Django’s choices field consists of two parts:

  • Value (Stored in Database) – The actual data saved in the database.
  • Display Label – The human-readable version shown in forms and admin panels.

For example, in a Django model:

# Define choices
DURATION_TYPE_CHOICE = ( 
    ("Hour", "1 Hour"), 
    ("Day", "1 Day"),
)

class Event(models.Model):
    duration = models.CharField(max_length=250, choices=DURATION_TYPE_CHOICE)

In this case, "Hour" is stored in the database, while "1 Hour" is displayed to users.

how to use

pip install validate_choice_field
from validate_choice_field import get_choice

print("If choice matches",get_choice(DURATION_TYPE_CHOICE,"1 Hour"))
# Output: "Hour"

print("If choice does not matches",get_choice(DURATION_TYPE_CHOICE,"Hour dcd"))
# Output: None

This function accepts two parameters:

  • choices: A list of tuples representing the available choices, where each tuple consists of a database value and its corresponding display value.

  • posted_value: The value submitted by the user, which needs to be validated.

The function compares the posted_value against the choices. If a match is found, it returns the corresponding database value. If no match is found, it returns None.

Purpose of This Package

When handling API requests, it is crucial to validate input values before saving them to the database. This package ensures that:

  • The submitted value exists in the predefined choices.
  • If a match is found, it returns the corresponding value for storage.
  • If no match is found, it returns None, preventing invalid data from being saved.

Benefits

  • Simplifies choice validation – Reduces manual validation logic.
  • Enhances data integrity – Ensures only valid choices are stored.
  • Improves code maintainability – Provides a reusable and structured approach for handling choice fields in Django applications.

conclusion

I hope this package proves beneficial to all developers. If you have any suggestions, corrections, or improvements, feel free to reach out via email at shalugassali@gmail.com. Looking forward to your feedback and contributions!

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

validate_choice_field-1.2.1.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

validate_choice_field-1.2.1-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file validate_choice_field-1.2.1.tar.gz.

File metadata

  • Download URL: validate_choice_field-1.2.1.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for validate_choice_field-1.2.1.tar.gz
Algorithm Hash digest
SHA256 a87cf71c2919d6a72e787ceb041c693bc97855468dc42a494c6438fef8ac8ce0
MD5 25a1dd440d2ea8572b36c2bc5a9cff79
BLAKE2b-256 10aca34ce82217abaacbd991f0855f7bf300533ad7d97020216fa8daa5e787c4

See more details on using hashes here.

File details

Details for the file validate_choice_field-1.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for validate_choice_field-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a2ffd96972412b7e4a5fa7720fa8aaeb7c00c4b81771d53da0589bc9e8ef442c
MD5 70a9bb82cd0ff7ca71a7544f0b02d27b
BLAKE2b-256 d34922756ff10166e3424305ea1cc124b071e62878aace4c9f6cbd1664cd703b

See more details on using hashes here.

Supported by

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