Changes between Version 1 and Version 2 of Ticket #35129, comment 4


Ignore:
Timestamp:
Jan 19, 2024, 10:16:32 AM (10 months ago)
Author:
Simon Charette

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #35129, comment 4

    v1 v2  
    1717
    1818Under the hood `ForeignKey` is `Field` + `ForeignObject` + the equivalent of a `ForeignKeyConstraint(ForeignObject)` entry in `Model._meta.constraints` so if you care about the ORM + JOIN generation part and don't want a database constraint it's likely what you're looking for.
     19
     20In other words, doing
     21
     22{{{#!python
     23class Journal(Model):
     24    issn = models.CharField(max_length=9, primary_key=True)
     25    name = models.CharField(max_length=255)
     26
     27class Biblio(Model):
     28    journal = models.ForeignKey(Journal, models.ON_DELETE, null=True)
     29}}}
     30
     31Is roughly the sugared equivalent of doing
     32
     33{{{#!python
     34class Journal(Model):
     35    issn = models.CharField(max_length=9, primary_key=True)
     36    name = models.CharField(max_length=255)
     37
     38class Biblio(Model):
     39    issn = models.CharField(max_length=50, null=True, db_index=True)
     40    journal = models.ForeignObject(
     41        Journal,
     42        models.ON_DELETE,
     43        from_fields=["issn"],
     44        to_fields=["issn"],
     45    )
     46    class Meta:
     47        constraints = [
     48            ForeignKeyConstraint(Journal, from_fields=["issn"], to_fields=["issn"])  # XXX: This currently doesn't exist but it could
     49        ]
     50}}}
Back to Top