{{{
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.
* status: new => closed
* resolution: => worksforme
* component: Uncategorized => Core (Serialization)
Comment:
Thanks for the ticket, however `"author": null` works for me (checked on
Django 4.2.2). You can try to use one of
[https://code.djangoproject.com/wiki/TicketClosingReasons/UseSupportChannels
support channels] when folks should help you debug.
--
Ticket URL: <https://code.djangoproject.com/ticket/34614#comment:1>