composite foreignkey support for django
Project description
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.2, 3.3, 3.4, 3.5
Django >= 1.8
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
Hashes for django-composite-foreignkey-1.0.0a10.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1743a23090696de21e956b481b0eae9e26eed51cca120f2b4ca456b25f32ed9b |
|
MD5 | 79d15c4c2e07a37784797bee4bccf899 |
|
BLAKE2b-256 | 21cc15d6daea0bc0d73634473d287d2844d666ca057ab770a75e053bd7295dff |
Hashes for django_composite_foreignkey-1.0.0a10-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 75fddbd84d04b53c96152545cedc728bfcb37f20f544cec0acb7770e5ad6454a |
|
MD5 | 69b14416f3e99de59440f5fe1d1e71d5 |
|
BLAKE2b-256 | 91698814dac1b6deff52ecc0ea90e6255e77e0da4cd2b6c2e66c5dd09e222d83 |