#28613: GenericForeignKey silently returns None when referring to non-existent pk. -------------------------------------+------------------------------------- Reporter: Sjoerd | Owner: nobody Job Postmus | Type: | Status: new Uncategorized | Component: | Version: master contrib.contenttypes | Severity: Normal | Keywords: genericforeignkey Triage Stage: | Has patch: 0 Unreviewed | Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 0 UI/UX: 0 | -------------------------------------+------------------------------------- We have a model as follows.
{{{ class RemoteIdentifierToLocal(models.Model): remote_id = models.UUIDField(primary_key=True) local_object_content_type = models.ForeignKey(ContentType) local_object_object_id = models.PositiveIntegerField() local_object = GenericForeignKey(ct_field='local_object_content_type', fk_field='local_object_object_id') }}} which is used as follows: {{{ if guid is not None: local_object = RemoteIdentifierToLocal.objects.get(remote_id=guid).local_object }}} To my surprise, we found a case where `guid` was not-None, and `local_object` was None. This was traced back to the implementation of `GenericForeignKey` containing a try/except-pass: https://github.com/django/django/blob/master/django/contrib/contenttypes/fields.py#L241 (now) https://github.com/django/django/commit/bca5327b21eb2e3ee18292cbe532d6d0071201d8 #diff-cc83843e623c1ab07a24317073330058R62 (initial commit, 11 years ago) Now, apparently this has been this way for 11 years already, so must be the "correct" behaviour. However I have not found any documentation of this fact, and would (myself) expect the attribute-access to *do* raise an exception or warning of some sort. -- Ticket URL: <https://code.djangoproject.com/ticket/28613> 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 django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/052.00aefbca37e044896369f978c5053289%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.