Skip to main content

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. Latest PyPI version Number of PyPI downloads per month

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.


  1. Install using pip:

    pip install django-composite-foreignkey

  2. Alternatively, you can install download or clone this repo and call

    pip install -e ..


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.


The full documentation is at


  • Python 2.7, 3.4, 3.5, 3.6, 3.7

  • Django 1.11, 2.0, 2.1

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.


You can use this under GPLv3.


Original author & Development lead: Darius BERNARD.


Thanks to django for this amazing framework. And thanks to django-bootstrap3 to the structure of the apps.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-composite-foreignkey-1.1.0.tar.gz (21.1 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page