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_function
from validate_choice_function 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.0.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.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: validate_choice_field-1.2.0.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.0.tar.gz
Algorithm Hash digest
SHA256 53c4adb299f74b3d7d2dcf40862b24b705156eaa4c025e0785417939f28482aa
MD5 1dbb904909ba2957953adee732ae7080
BLAKE2b-256 b4ead4f884307c8a9bf46bc97dcb9f4095f2b6fa2e2f0e90396d78bc5bec03c2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for validate_choice_field-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7178d4bd956d8ef672e93f1e5a95d19d5562a56ccee04814074a7e0320b865a1
MD5 793f8ac31b9496c80158e3515dfe6a1f
BLAKE2b-256 a736a72f76ba96223aed68d315903b93195738380def8fe26af3e78cc3c147c6

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