pondelok 7. septembra 2009

How to set multi-field primary key

I have class AccountingDocument and want to have combination of Type, Issuer and Identifier unique. Howto is here.

The implementation:
class AccountingDocument(models.Model):
type = models.ForeignKey('AccountingDocumentType', help_text='Typ účtovného dokladu.')
identifier = models.CharField(max_length=40, help_text='Číslo účtovného dokladu, jedinečný identifikátor, ktorý mu jeho vystavovateľ pridelil, napr. číslo faktúry.')
  date_issued = models.DateField()
  time_issued = models.TimeField(null=True, blank=True)
  currency = models.ForeignKey('Currency')
  value_without_VAT = models.FloatField(help_text='Suma účtovného dokladu bez DPH.')
  VAT_rate = models.FloatField(default=19, help_text='Sadzba DPH.')
issuer = models.ForeignKey('BusinessUnit', related_name='AD_issuer')
  recipient = models.ForeignKey('BusinessUnit', null=True, blank=True, related_name='AD_recipient')
  scanned_items_file = models.FileField(null=True, blank=True, upload_to=settings.ACCOUNTING_DOCUMENTS_FILE_PATH)
class Meta:
unique_together = ("type", "identifier", "issuer")
# This is a list of lists of fields that must be unique when considered together.
# It's used in the Django admin and is enforced at the database level (i.e., the appropriate UNIQUE statements are included in the CREATE TABLE statement).
# For convenience, unique_together can be a single list when dealing with a single set of fields.
That's it, no evolution needed, just run python manage.py syncdb in project directory.

Žiadne komentáre:

Zverejnenie komentára

Archív blogu