Skip to main content

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

  1. Add extra_fields to your INSTALLED_APPS:

    In your Django project's settings.py, add extra_fields to INSTALLED_APPS:

    INSTALLED_APPS = [
        ...
        'extra_fields',
    ]
    
  2. 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)
    
  3. Migrate Your Database:

    Create and apply migrations for the new extra_fields tables:

    python manage.py makemigrations
    python manage.py migrate
    
  4. 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')
    
  5. 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)
    
  6. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_extra_model_fields-1.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

django_extra_model_fields-1.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file django_extra_model_fields-1.0.tar.gz.

File metadata

File hashes

Hashes for django_extra_model_fields-1.0.tar.gz
Algorithm Hash digest
SHA256 f9b92f472cc87d6e67c30d5393b6606885b24c04f05cf0d9fb97d55588cc11a1
MD5 28efbfb87cad81a0fa9bfcaad35a3c00
BLAKE2b-256 bebbc7a03e8678c8a3c39251ce6a2e39c512753e54da21aea9f63a0fc14a5edd

See more details on using hashes here.

File details

Details for the file django_extra_model_fields-1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_extra_model_fields-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7d1c53dad6bb68187fd2c12d877dbddc1a42d337c58387b9aa0a5b85a5b8d652
MD5 d526522e1fcb678ed8850bdcfdcb0008
BLAKE2b-256 9036cd4274de267ebcc119e13e890224a1ec21b238d622ac2d0b8c965ea9fd35

See more details on using hashes here.

Supported by

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