Convert Figma logo to code with AI

jazzband logodjango-model-utils

Django model mixins and utilities.

2,648
365
2,648
105

Top Related Projects

79,088

The Web framework for perfectionists with deadlines.

This is a repository for collecting global custom management extensions for the Django Framework.

Dynamic Django settings.

A generic system for filtering Django QuerySets based on user selections

Silky smooth profiling for Django

Web APIs for Django. 🎸

Quick Overview

Django-model-utils is a collection of utilities and mixins for Django models. It provides a set of tools to enhance Django's ORM functionality, making it easier to work with models and perform common operations. The library aims to simplify common patterns and reduce boilerplate code in Django projects.

Pros

  • Offers useful mixins like TimeStampedModel and SoftDeletableModel, saving development time
  • Provides flexible field types such as StatusField and MonitorField for advanced model behaviors
  • Maintains compatibility with multiple Django versions, ensuring long-term usability
  • Well-documented and actively maintained by the Jazzband community

Cons

  • May introduce additional dependencies to your project
  • Some features might be overkill for smaller projects or simpler use cases
  • Learning curve for developers unfamiliar with the library's concepts
  • Potential performance impact when using certain features extensively

Code Examples

  1. Using TimeStampedModel:
from model_utils.models import TimeStampedModel

class Article(TimeStampedModel):
    title = models.CharField(max_length=200)
    content = models.TextField()

# This model will automatically have 'created' and 'modified' fields
  1. Implementing SoftDeletableModel:
from model_utils.models import SoftDeletableModel

class Comment(SoftDeletableModel):
    text = models.TextField()
    author = models.ForeignKey(User, on_delete=models.CASCADE)

# Deleting a comment will set 'is_removed' to True instead of actually deleting it
  1. Using StatusField:
from model_utils.models import StatusModel
from model_utils import Choices

class Order(StatusModel):
    STATUS = Choices('created', 'paid', 'shipped', 'returned')
    
    # This will create a status field with the defined choices

Getting Started

To use django-model-utils in your project:

  1. Install the package:

    pip install django-model-utils
    
  2. Add 'model_utils' to your INSTALLED_APPS in settings.py:

    INSTALLED_APPS = [
        # ...
        'model_utils',
        # ...
    ]
    
  3. Import and use the desired utilities in your models:

    from model_utils.models import TimeStampedModel
    
    class MyModel(TimeStampedModel):
        # Your model fields and methods here
    
  4. Run migrations to apply any changes:

    python manage.py makemigrations
    python manage.py migrate
    

Competitor Comparisons

79,088

The Web framework for perfectionists with deadlines.

Pros of Django

  • Comprehensive web framework with built-in features for routing, ORM, authentication, and more
  • Extensive documentation and large community support
  • Regular updates and long-term support

Cons of Django

  • Steeper learning curve for beginners due to its full-stack nature
  • Can be overkill for small projects or microservices
  • Less flexibility in some areas compared to more modular approaches

Code Comparison

Django (models.py):

from django.db import models

class MyModel(models.Model):
    name = models.CharField(max_length=100)
    created_at = models.DateTimeField(auto_now_add=True)

django-model-utils (models.py):

from django.db import models
from model_utils.models import TimeStampedModel

class MyModel(TimeStampedModel):
    name = models.CharField(max_length=100)

Key Differences

  • Django is a full-featured web framework, while django-model-utils is a utility package for Django models
  • django-model-utils provides additional model fields and behaviors not included in Django's core
  • Django-model-utils can simplify common model patterns and reduce boilerplate code
  • Django offers more control over model fields and behaviors, while django-model-utils provides pre-built solutions

Use Cases

  • Choose Django for full-stack web applications requiring a comprehensive framework
  • Use django-model-utils alongside Django to enhance model functionality and reduce repetitive code
  • Consider django-model-utils for projects heavily focused on model-related features and rapid development

This is a repository for collecting global custom management extensions for the Django Framework.

Pros of django-extensions

  • Broader scope with a wide range of management commands and utilities
  • Includes development tools like shell_plus and runserver_plus
  • Offers additional template tags and database fields

Cons of django-extensions

  • Larger footprint and potentially more complex to integrate
  • Some features may overlap with built-in Django functionality
  • Requires more frequent updates due to its broader scope

Code Comparison

django-extensions:

from django_extensions.db.models import TimeStampedModel

class MyModel(TimeStampedModel):
    name = models.CharField(max_length=100)

django-model-utils:

from model_utils.models import TimeStampedModel

class MyModel(TimeStampedModel):
    name = models.CharField(max_length=100)

Both libraries provide similar functionality for timestamped models, but django-extensions offers a wider range of features beyond model utilities.

django-extensions is more suitable for projects requiring extensive development tools and utilities, while django-model-utils is focused specifically on enhancing Django models with additional fields and behaviors. Choose based on your project's specific needs and the level of additional functionality required.

Dynamic Django settings.

Pros of django-constance

  • Provides a simple way to store dynamic settings in the database
  • Offers a user-friendly admin interface for managing configuration values
  • Supports different backends (database, redis) for storing settings

Cons of django-constance

  • Limited to storing simple configuration values, not complex data structures
  • Requires additional setup and configuration compared to django-model-utils
  • May introduce performance overhead when frequently accessing settings

Code Comparison

django-constance:

from constance import config

# Accessing a configuration value
debug_mode = config.DEBUG_MODE

# Updating a configuration value
config.SITE_NAME = "My Django Site"

django-model-utils:

from model_utils.models import TimeStampedModel

class MyModel(TimeStampedModel):
    name = models.CharField(max_length=100)
    
# Automatically adds created and modified fields

django-constance focuses on dynamic configuration management, while django-model-utils provides utility classes and mixins for Django models. The former is better suited for applications requiring runtime configuration changes, while the latter is more useful for enhancing model functionality and reducing boilerplate code in Django projects.

A generic system for filtering Django QuerySets based on user selections

Pros of django-filter

  • Specialized for filtering querysets, providing a more comprehensive and flexible filtering system
  • Integrates well with Django REST Framework for API filtering
  • Offers a wide range of filter types out-of-the-box (e.g., date range, numeric range, text search)

Cons of django-filter

  • Focused solely on filtering, lacking other model utility features
  • May require additional setup and configuration for complex filtering scenarios
  • Learning curve can be steeper for developers new to advanced filtering concepts

Code Comparison

django-filter:

class ProductFilter(django_filters.FilterSet):
    name = django_filters.CharFilter(lookup_expr='icontains')
    price = django_filters.NumberFilter()
    price__gt = django_filters.NumberFilter(field_name='price', lookup_expr='gt')
    price__lt = django_filters.NumberFilter(field_name='price', lookup_expr='lt')

django-model-utils:

from model_utils.models import TimeStampedModel

class Product(TimeStampedModel):
    name = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=10, decimal_places=2)

While django-filter focuses on creating flexible filtering options for querysets, django-model-utils provides utility mixins and fields for Django models, such as TimeStampedModel in this example. The choice between these libraries depends on the specific needs of your project, with django-filter being more suitable for advanced filtering requirements and django-model-utils offering a broader range of model-related utilities.

Silky smooth profiling for Django

Pros of django-silk

  • Provides detailed profiling and inspection of HTTP requests and database queries
  • Offers a user-friendly web interface for analyzing performance data
  • Supports real-time monitoring of Django applications

Cons of django-silk

  • May introduce performance overhead in production environments
  • Requires additional setup and configuration compared to django-model-utils
  • Has more dependencies and a larger codebase

Code Comparison

django-silk:

from silk.profiling.profiler import silk_profile

@silk_profile(name='View Blog Post')
def view_blog_post(request, post_id):
    # View logic here

django-model-utils:

from model_utils.models import TimeStampedModel

class BlogPost(TimeStampedModel):
    title = models.CharField(max_length=100)
    content = models.TextField()

django-silk focuses on request profiling and performance analysis, while django-model-utils provides utility mixins and fields for Django models. The code examples demonstrate their different use cases: django-silk for profiling views and django-model-utils for enhancing model functionality.

Web APIs for Django. 🎸

Pros of Django REST Framework

  • Comprehensive toolkit for building Web APIs, including serializers, views, and authentication
  • Extensive documentation and large community support
  • Browsable API for easier testing and development

Cons of Django REST Framework

  • Steeper learning curve due to its extensive feature set
  • Can be overkill for simple projects that don't require full API functionality
  • Potential performance overhead for large-scale applications

Code Comparison

Django REST Framework:

from rest_framework import serializers

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['id', 'username', 'email']

Django Model Utils:

from model_utils.models import TimeStampedModel

class User(TimeStampedModel):
    username = models.CharField(max_length=100)
    email = models.EmailField()

Django REST Framework focuses on API development, providing tools for serialization and view handling. Django Model Utils, on the other hand, offers utility mixins and fields to enhance Django models. While Django REST Framework is more suited for building complex APIs, Django Model Utils is beneficial for extending model functionality in general Django projects.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

================== django-model-utils

.. image:: https://jazzband.co/static/img/badge.svg :target: https://jazzband.co/ :alt: Jazzband .. image:: https://github.com/jazzband/django-model-utils/workflows/Test/badge.svg :target: https://github.com/jazzband/django-model-utils/actions .. image:: https://codecov.io/gh/jazzband/django-model-utils/branch/master/graph/badge.svg :target: https://codecov.io/gh/jazzband/django-model-utils .. image:: https://img.shields.io/pypi/v/django-model-utils.svg :target: https://pypi.python.org/pypi/django-model-utils .. image:: https://img.shields.io/pypi/pyversions/django-model-utils.svg :target: https://pypi.python.org/pypi/django-model-utils :alt: Supported Python versions .. image:: https://img.shields.io/pypi/djversions/django-model-utils.svg :target: https://pypi.org/project/django-model-utils/ :alt: Supported Django versions

Django model mixins and utilities.

django-model-utils supports Django_ 3.2+.

.. _Django: https://www.djangoproject.com/

This app is available on PyPI_.

.. _PyPI: https://pypi.python.org/pypi/django-model-utils/

Getting Help

Documentation for django-model-utils is available https://django-model-utils.readthedocs.io/

Contributing

Please file bugs and send pull requests to the GitHub repository_ and issue tracker. See CONTRIBUTING.rst for details.

.. _GitHub repository: https://github.com/jazzband/django-model-utils/ .. _issue tracker: https://github.com/jazzband/django-model-utils/issues .. _CONTRIBUTING.rst: https://github.com/jazzband/django-model-utils/blob/master/CONTRIBUTING.rst