Composite Foreign Key Field

viewflow.fields.CompositeKey - Virtual field allows Django to get access to database tables with ForeignKey. The field does not provide support for migrations, and suitable only for Meta.managed = False models only

Usage

To use CompositeKey, import it from viewflow.fields and define it in your model. The following example demonstrates how to use CompositeKey with a Seat model, which references an Aircraft model via a composite key consisting of aircraft_code and seat_no.

from viewflow.fields import CompositeKey

class Seat(models.Model):
    id = CompositeKey(columns=['aircraft_code', 'seat_no'])
    aircraft_code = models.ForeignKey(Aircraft, models.DO_NOTHING)
    seat_no = models.CharField(max_length=4)

class Meta:
    managed = False
    db_table = 'aircrafts_data'

Important Notes

  • The CompositeKey field is virtual and does not provide support for database migrations. It should only be used in models where Meta.managed = False.
  • Ensure that the db_table attribute in the Meta class matches the actual table name in the database.

See also