Skip to main content

A field type for Django models that allows joins to a related model without a foreign key.

Project description

https://travis-ci.org/martsberger/django-joinfield.svg?branch=master

Django JoinField

This package provides a field type for Django models that allows joins to a related model without a foreign key.

Quickstart

Install via pip:

pip install django-joinfield

Put joinfield in INSTALLED_APPS in your settings file. Then you can import JoinField and use it when defining your models:

from joinfield.joinfield import JoinField

class Parent(models.Model):
    column = models.CharField(max_length=64)


class Child(models.Model):
    parent = JoinField(Parent, to_field='column')

The database column created in the child table will have the type defined by the to_field parent column. In this case, a CharField. It will not be a foreign key and there will be no database constraints between these two tables.

Now you can do joins between child and parent using the orm:

parent = Parent.objects.first()
children = Child.objects.filter(parent=parent)

# Or
children = Child.objects.filter(parent__column='some value')

Examples

Let’s imagine a database of people and family information. One table might contain general family information keyed by Surname:

class Surname(models.Model):
    name = models.CharField(max_length=32, primary_key=True)
    crest = models.ImageField(...)
    references = models.ManyToManyField('FamilyDocuments', ...)
    origin = models.ForeignKeyField('Country')

A separate table contains the individual people. Each person has a last name and if that last name corresponds to a Surname in the Surname table, we want to be able to join from Person to Surname. However, not every person’s last name will correspond to a Surname that we have detailed information for. So we don’t want to require a record in the Surname table in order to create a Person. The person class will use the JoinField:

class Person(models.Model):
    first_name = models.CharField(max_length=32)
    last_name = JoinField(Surname, on_delete=models.DO_NOTHING)

This will result in the database column for Person.last_name being a CharField just like it is defined on Surname.name.

A Person object can be created by assigning either a Surname object or a literal value to the last_name attribute:

# Create a Person object with a literal value for last name
Person.objects.create(first_name='Jon', last_name='Doe')

# A Surname object can be created after the fact
doe = Surname.objects.create(name='Doe')

# A person can be created by passing a Surname object as the last_name
Person.objects.create(first_name='Jane', last_name=doe)

The ORM can be used for both forward and reverse relationships:

# These two queries are equivalent
Person.objects.filter(last_name='Doe')  # literal filter
Person.objects.filter(last_name=doe)  # filter on forward relationship

# The reverse relationship can also be used for filtering
Surname.objects.filter(person__first_name='Jon')

# And annotations
Surname.objects.annotate(count=Count('person'))

The _id field

Other than the lack of database constraints (which provides the ability to assign a literal in addition to an instance), the JoinField is very much like a ForeignKey field. The value that is stored in the database is the literal value assigned or the value of the field on the instance we join to. When an instance is retrieved, the attribute defined as a JoinField will have the instance we join to as it’s value. An additional attribute with “_id” appended stores the literal value.

From the example above, when a Person instance is retrieved, the last_name attribute will be a Surname instance and the last_name_id attribute will be the actual value of the last_name. For Person instances that don’t join to any value in the Surname table (not valid for ForeignKey), the last_name attribute will be None and the last_name_id attribute will still have the literal last_name value.

For example:

jon = Person.objects.get(first_name='Jon', last_name='Doe')
print (jon.first_name, jon.last_name, jon.last_name_id)

Will print the following:

(u'Jon', <Surname: Surname object>, u'Doe')

And if we create a Person with no Surname:

james = Person.objects.create(first_name='James', last_name='Smith')
print (james.first_name, james.last_name, james.last_name_id)

prints the following:

(u'James', None, u'Smith')

License

MIT

Copyright 2023 Brad Martsberger

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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_joinfield-0.2.1.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

django_joinfield-0.2.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file django_joinfield-0.2.1.tar.gz.

File metadata

  • Download URL: django_joinfield-0.2.1.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for django_joinfield-0.2.1.tar.gz
Algorithm Hash digest
SHA256 d536f3c26e0b0465c7659b371094bd1c0a8b4f0c586a85ffb4f7c0c02207a9a4
MD5 dc8016cc6b3b0c349e1085e46a7479e5
BLAKE2b-256 73bcf4d322d9b266e07512fe63398b6ed40b7adec5ccca943e9d946c745a92b8

See more details on using hashes here.

File details

Details for the file django_joinfield-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_joinfield-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5d75d3815ae279ec861f8e401941e12ceb0bd0dd09990d54fa7290de70d0da55
MD5 655d9be3114bfb6c57421299a4797e98
BLAKE2b-256 efeb8d54eb54e2a083ac3205896fe3785a5fe20f30327153685a0e3402937502

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page