django-multiforeignkey
Django ForeignKey that links to one of several specified models:
class Comment(models.Model):
user = models.ForeignKey(User)
text = models.TextField()
# users may leave comments for blog posts, articles or votings
object = MultiForeignKey(Post, Article, Voting)
Unlike GenericForeignKey, the field uses native database foreign keys and provides maximum speed and reliability.
WARNING
This is a preliminary alpha version which most probably doesn’t fully work as advertised. I had to publish it unfinished because I had to use it in my project.
Installation
pip install django-multiforeignkey
Usage
Add multiforeignkey to INSTALLED_APPS:
# settings.py
INSTALLED_APPS = [
...
'multiforeignkey',
]
Add a field to your models:
# comments/models.py
from multiforeignkey.models import MultiForeignKey
class Comment(models.Model):
user = models.ForeignKey(User)
text = models.TextField()
# users may leave comments for blog posts, articles or votings
object = MultiForeignKey(Post, Article, Voting)
Create the corresponding database tables:
./manage.py makemigrations && ./manage.py migrate
Use the new field as if it were a real database field:
comment = Comment.objects.filter(object=post)
assert comment.object is post
comment.object = voting
assert comment.object is voting
comment.save()
or use specific subfields:
comment = Comment.objects.filter(post=post)
assert comment.post is post
assert comment.article is None
assert comment.voting is None
comment.voting = voting
assert comment.post is None
assert comment.article is None
assert comment.voting is voting
comment.save()
Metadata
Release files for django-multiforeignkey 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django-multiforeignkey-0.0.2.tar.gz | 3.2 kB | Details |
Release files / django-multiforeignkey-0.0.2.tar.gz
| Download URL | django-multiforeignkey-0.0.2.tar.gz |
|---|---|
| Size | 3.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
930d4c22dac2ae5bc0bf2e99d072d0039834e4e0c750463e29f35a7f792d3ee9
|
|
BLAKE2b-256 checksum How to use checksums |
9b67104c1b431a1b6b813ed78c343097261d4052700b6804efa57f18cec9e594
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |