Skip to main content

django-generic-helpers

https://badge.fury.io/py/django-generic-helpers.svg https://coveralls.io/repos/marazmiki/django-generic-helpers/badge.svg?branch=master https://pypip.in/d/django-generic-helpers/badge.png Wheel Status Supported Python versions Supported Django versions

The application provides some syntax sugar for working with Django’s generic relations

Installation

Just install the package from PyPI within pip

pip install django-generic-helpers

…or pipenv

pipenv install django-generic-helpers

…or even poetry

poetry add django-generic-helpers

That’s all. No need to add this into INSTALLED_APPS of your project or something like that.

Usage

That’s how did you work with generic relations before:

# models.py
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models

class Post(models.Model):
    pass

class Image(models.Model):
     content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
     object_id = models.IntegerField()
     content_object = GenericForeignKey(ct_field='content_type', fk_field='object_id')

# Example of filtering
post = Post.objects.get(pk=1)
images = Image.objects.filter(
    content_type=ContentType.objects.get_for_object(post),
    object_id=post.id
)

Looks verbose a bit, yep? Let’s rewrite this with django-generic-helpers

# models.py
from django.db import models
from generic_helpers.fields import GenericRelationField

class Post(models.Model):
    pass

class Image(models.Model):
     content_object = GenericRelationField()

# Example of filtering
post = Post.objects.get(pk=1)
images = Image.objects.filter(content_object=post)

Personally, I found it much simpler and cleaner.

Features the application provides:

  • Creating an arbitrary number of generic relation fields, both required and optional;

  • Providing custom names for content_type and object_id columns

  • You can define a whitelist (or a black one) of models that could (not) be written into the field

Please, follow up the documentation for details.

Contributing

A few words if you plan to send a PR:

  • Please, write tests!

  • Follow PEP-0008 codestyle recommendations.

  • When pushing, please wait while Travis CI will finish his useful work and complete the build. And if the build fails, please fix the issues before PR

  • And of course, don’t forget to add yourself into the authors list ;)

License

The license is MIT.

Metadata

Release files for django-generic-helpers 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-generic-helpers 1.2.0
File Size Uploaded
django-generic-helpers-1.2.0.tar.gz 9.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-generic-helpers 1.2.0
File Interpreter ABI Platform
django_generic_helpers-1.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 19.2 kB

Release files / django-generic-helpers-1.2.0.tar.gz

Download URL django-generic-helpers-1.2.0.tar.gz
Size 9.6 kB
Tags Source
SHA-256 checksum
How to use checksums
b3ed133524b2d7d30f333036be18000b5805ea02ec952059cf9638da4138ccf0
BLAKE2b-256 checksum
How to use checksums
5c5d37b033eb40c38e11ba11106d4f1ef52f1ece898f7abd1227a8abe8ce4455
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.11 CPython/3.10.0 Linux/5.4.0-91-generic

Release files / django_generic_helpers-1.2.0-py3-none-any.whl

Download URL django_generic_helpers-1.2.0-py3-none-any.whl
Size 9.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
094dff395f472be3c2c61d2d55f00af0c45755c789ac695c70c1124eaec8c5b5
BLAKE2b-256 checksum
How to use checksums
bb3a18affcce58b7798a6526b40e6a3c84e58cb8ca515e4d950057eb8efd69be
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.11 CPython/3.10.0 Linux/5.4.0-91-generic

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.3.7

4 release files

0.3.6

4 release files

0.3.5

4 release files

0.3.4

4 release files

0.3.3

4 release files

0.3.2

4 release files

0.3.1

3 release files

0.3.0

3 release files

0.2.2

3 release files

0.2.1

1 release file

0.2

1 release file

0.1.1

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page