Linked tables/models in Django - more than Foreign Keys

I am designing a Django project, where I need the following (I want to implement something more complex, but simplified here):

Table: work_order
Columns: owner_id,
         ...,
         contract_file_id (maps to 'all_files.id')

Table: labour_license
Columns: owner_id,
         ...,
         license_file_id (maps to 'all_files.id')

Table: all_files
Columns: id (UUID4Field),
         category (CharField,
                  'contract' if linked to 'work_order.contract_file_id',
                  'labour_lic' if linked to 'labour_license.license_file_id')
         file (FileField)
         ...
  • Anytime a file is uploaded to work_order, it should insert a row in all_files with category='contract'; similarly for labour_license.
  • Anytime a file with a particular UUID4 is to be accessed, I would like to query back the owner_id (in work_order table if all_files.category='contract'; similarly for labour_license.

I have literally 10s of file fields like this, and I would like to write some kind of Mixin or custom Field, or even a metaclass if required, which I can use for all such file fields. If it is possible, would appreciate some ideas or directions I can look into.

Вернуться на верх