#34614: Natural keys for nullable foreign keys
-----------------------------------------+------------------------
Reporter: martinberoiz | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 4.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
I found that if a fixture has a model with a nullable foreign key (FK) to
a model with natural keys, loaddata fails to load the first object.
For example, slightly modifying the docs example:
{{{
class PersonManager(models.Manager):
def get_by_natural_key(self, name):
return self.get(name=name)
class Person(models.Model):
name = models.CharField(max_length=100, unique=True)
objects = PersonManager()
def natural_key(self):
return (self.name,)
class Book(models.Model):
name = models.CharField(max_length=100)
author = models.ForeignKey(Person, on_delete=models.CASCADE,
null=True)
}}}
(notice the null=True addition to Book)
Then this fixture will fail when attempting to load with loaddata
{{{
[
{
"model": "myapp.book",
"fields": {
"name": "I Ching",
"author": null,
}
},
]
}}}
Notice that "author": [null] in the fixture does not fix it either, since
it will pass None to the manager's get_by_natural_key. Returning None from
get_by_natural_key doesn't help either.
If this is a feature, and models with natural keys can't ever be
referenced by other models with a _nullable_ FK to them, I think it should
probably be explicit in the documentation.
--
Ticket URL: <https://code.djangoproject.com/ticket/34614>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107018873530542-92b38234-75e4-4ee6-b305-d4bffecde274-000000%40eu-central-1.amazonses.com.