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.

🚀 Installation

Install the package via pip:

pip install my_package

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-0.1.0.tar.gz (3.5 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-0.1.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: validate_choice_field-0.1.0.tar.gz
  • Upload date:
  • Size: 3.5 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-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4c898d777930dfacbcea46e0fd61dcaf5f49b84f38d62466182ada852234c5c4
MD5 5ef24c5f95e8e831b8eac1c2f7a5888d
BLAKE2b-256 37c269844b78c76bd9630550456a353f29d31c28e67b758f15320fc410112ad1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for validate_choice_field-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0185b76e704841c2211e06bf4952b579253bcf0397e3226fcce5d0f80955880a
MD5 e9cfcb1be2e4d2710d7808df3b54cdf7
BLAKE2b-256 3caf7b1a9e5f62e8ae080f4f6bfaa8b0e3e714c634c8816f3f67ee6fd0d1a1b1

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