Some of my models
are only unique in a combination of keys
. I don't want to use an auto-numbering
id
as the identifier as subsets of the data will be exported to other systems (such as spreadsheets
), modified and then used to update the master database
.
Here's an example:
class Statement(models.Model):
supplier = models.ForeignKey(Supplier)
total = models.DecimalField("statement total", max_digits=10, decimal_places=2)
statement_date = models.DateField("statement date")
....
class Invoice(models.Model):
supplier = models.ForeignKey(Supplier)
amount = models.DecimalField("invoice total", max_digits=10, decimal_places=2)
invoice_date = models.DateField("date of invoice")
statement = models.ForeignKey(Statement, blank=True, null=True)
....
Invoice
records are only unique for a combination of supplier
, amount
and invoice_date
I'm wondering if I should create a slug
for Invoice
based on supplier
, amount
and invoice_date
so that it is easy to identify the correct record.
An example of the problem of having multiple related fields
to identify the right record is django-csvimport
which assumes there is only one related field and will not discriminate on two when building the foreign key links
.
Yet the slug
seems a clumsy option and needs some kind of management to rebuild the slugs
after adding records
in bulk.
I'm thinking this must be a common problem and maybe there's a best practice design pattern out there somewhere.
I am using PostgreSQL
in case anyone has a database solution. Although I'd prefer to avoid that if possible, I can see that it might be the way to build my slug
if that's the way to go, perhaps with trigger functions
. That just feels a bit like hidden functionality though, and may cause a headache for setting up on a different server.
UPDATE - after reading initial replies
My application requires that data may be exported, modified remotely, and merged back into the master database after review and approval. Hidden autonumber keys don't easily survive that consistently. The relation invoices[2417] is part of statements[265]
is not persistent if the statement
table was emptied and reloaded from a CSV
.
If I use the numeric autonumber pk
then any process that is updating the database
would need to refresh the related key numbers or by using the multiple WITH clause.
If I create a slug that is based on my 3 keys but easy to reproduce then I can use it as the key - albeit clumsily. I'm thinking of a slug along the lines:
u'%s %s %s' % (self.supplier,
self.statement_date.strftime("%Y-%m-%d"),
self.total)
This seems quite clumsy and not very DRY
as I expect I may have to recreate the slug elsewhere duplicating the algorithm (maybe in an Excel
formula, or an Access
query)
I thought there must be a better way I'm missing but it looks like yuvi's reply means there should be, and there will be, but not yet :-(