A Django app for adding and managing extra fields on any model.
Project description
django-extra-model-fields
django-extra-model-fields
is a Django package that allows you to attach and manage additional fields on any model dynamically. This package is designed to be simple and flexible, enabling you to add key-value pairs to any Django model without modifying the original model structure.
Features
- Add custom key-value pairs to any Django model.
- Query and manage these fields easily.
- Supports Django's admin interface for easier management.
Installation
You can install django-extra-model-fields
via pip. Add it to your requirements.txt
or install it directly using pip:
pip install django-extra-model-fields
Usage
Adding to Your Django Project
-
Add extra_fields to your INSTALLED_APPS:
In your Django project's settings.py, add extra_fields to INSTALLED_APPS:
INSTALLED_APPS = [ ... 'extra_fields', ]
-
Extend Your Models:
To add extra fields to a model, make your model inherit from ExtraFieldModel:
from django.db import models from extra_fields.models import ExtraFieldModel class MyModel(ExtraFieldModel): name = models.CharField(max_length=100)
-
Migrate Your Database:
Create and apply migrations for the new extra_fields tables:
python manage.py makemigrations python manage.py migrate
-
Use the Extra Fields:
You can now use the set_extra_field, get_extra_field, and delete_extra_field methods to manage extra fields:
blog = Blog.objects.create(title="Intorduction to Computers", content="<p>blog body goes here</p>") # setting key value blog.set_value('author', 'John Smith') blog.set_value('meta_data', {'clicks': 100, 'views': 200}) # getting values blog.get_value('author') # returns a string: 'John Smith' blog.get_value('meta_data') # returns a python dict: {'clicks': 100, 'views': 200} # getting all keys blog.get_keys() # return a queryset ['author', 'meta_data'] # deleting a key-value pair blog.delete_key('author')
-
Filtering
# list all extra_fields blog.extra_fields.all() # returns a query set of <ExtraField> # filter on extra_fields Blog.objects.filter(extra_fields__key='author', extra_fields__value='John Smith') Blog.objects.filter(extra_fields__value__clicks__gte=100)
-
Add Inline editor on Admin panel
Add ExtraFieldModelInline in inlines of the model Admin
from django.contrib import admin from .models import Blog from extra_field.admin import ExtraFieldModelInline class BlogAdmin(admin.ModelAdmin): """Admin for blog model.""" list_display = ('title', 'content') inlines = [ExtraFieldModelInline] admin.site.register(Blog, BlogAdmin)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django_extra_model_fields-1.0.tar.gz
.
File metadata
- Download URL: django_extra_model_fields-1.0.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f9b92f472cc87d6e67c30d5393b6606885b24c04f05cf0d9fb97d55588cc11a1 |
|
MD5 | 28efbfb87cad81a0fa9bfcaad35a3c00 |
|
BLAKE2b-256 | bebbc7a03e8678c8a3c39251ce6a2e39c512753e54da21aea9f63a0fc14a5edd |
File details
Details for the file django_extra_model_fields-1.0-py3-none-any.whl
.
File metadata
- Download URL: django_extra_model_fields-1.0-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7d1c53dad6bb68187fd2c12d877dbddc1a42d337c58387b9aa0a5b85a5b8d652 |
|
MD5 | d526522e1fcb678ed8850bdcfdcb0008 |
|
BLAKE2b-256 | 9036cd4274de267ebcc119e13e890224a1ec21b238d622ac2d0b8c965ea9fd35 |