composite foreignkey support for Django 4.x
Project description
### Note
The original project appears to be abandoned. My intent is to solve my immediate issues while learning a bit more about the underlying Django DB model and then continue to update this library.
django-composite-foreignkey
allow to create a django foreignkey that don’t link with pk of other model, but with multi column matching local model columns or fixed values.
some databases have a composite Primary Key, leading to impossiblity for a django foreign key to be used.
today, Django don’t support Composite Primary Key see ticket and ForeignKey don’t support multicolumn. but fortunaly, the base class of ForeignKey support it well, so this lib just add a little wrapper around ForeignObject to make it more usefull. the real add of this implementation is that is support the customisation of the link with Raw values.
this implementation of CompositeForeignKey skip the complexity of Composite Primary Key by forcing the providing of the corresponding column of the other model, not forcefully a PrimaryKey.
Installation
Install using pip:
pip install django-composite-foreignkey
Alternatively, you can install download or clone this repo and call
pip install -e ..
Example
you have this model
class Customer(models.Model):
company = models.IntegerField()
customer_id = models.IntegerField()
name = models.CharField(max_length=255)
address = CompositeForeignKey(Address, on_delete=CASCADE, to_fields={
"tiers_id": "customer_id",
"company": LocalFieldValue("company"),
"type_tiers": RawFieldValue("C")
})
class Meta(object):
unique_together = [
("company", "customer_id"),
]
class Contact(models.Model):
company_code = models.IntegerField()
customer_code = models.IntegerField()
surname = models.CharField(max_length=255)
# virtual field
customer = CompositeForeignKey(Customer, on_delete=CASCADE, related_name='contacts', to_fields={
"customer_id": "customer_code",
"company": "company_code"
})
you can use Contact.customer like any ForeignKey, but behinde the scene, it will query the Customer Table using company and customer id’s.
Documentation
The full documentation is at http://django-composite-foreignkey.readthedocs.org/en/latest/.
Requirements
Python 2.7, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9
Django 1.11, 2.0, 2.1, 2.2, 3.1, 3.2
Contributions and pull requests for other Django and Python versions are welcome.
Bugs and requests
If you have found a bug or if you have a request for additional functionality, please use the issue tracker on GitHub.
https://github.com/onysos/django-composite-foreignkey/issues
License
You can use this under GPLv3.
Thanks
Thanks to django for this amazing framework. And thanks to django-bootstrap3 to the structure of the apps.
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-composite-foreignkey-2-3.9.0.tar.gz
.
File metadata
- Download URL: django-composite-foreignkey-2-3.9.0.tar.gz
- Upload date:
- Size: 22.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 37862dae3671cad0ef885424cf9867cbcd4de68f04ebbfaab37ea0b1418562b2 |
|
MD5 | 74c83790e551060286d0766863b5b323 |
|
BLAKE2b-256 | df5ec78773195d9926022d0fb4b4288622333728078e28b784df8b352e7d820f |
File details
Details for the file django_composite_foreignkey_2-3.9.0-py2.py3-none-any.whl
.
File metadata
- Download URL: django_composite_foreignkey_2-3.9.0-py2.py3-none-any.whl
- Upload date:
- Size: 20.5 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0e1570e2d9578e59c34fdcd25a9568021f2279b6e929445a0181f0af594e95b1 |
|
MD5 | 8617af9260755f52e65e68d53832c438 |
|
BLAKE2b-256 | 0639f9af58cf4cefca2a69f0aec9c58b171fe9de5c6285b7944de3ae1b3e4050 |