Monday, March 29, 2021

Django Admin 3.1.7: getting RelatedObjectDoesNotExist for required ForeignKey

Hello,

I'm using Django 3.1.7's Admin form of the following model:

class EthernetInterface(models.Model):
    slug_name = models.CharField(max_length=32)
    MAC = MACAddressField(blank=True, null=True)
    lan = models.ForeignKey(LAN, on_delete=models.PROTECT, related_name='lan_interfaces')
    server_host = models.ForeignKey(ServerHost, on_delete=models.PROTECT, related_name='interfaces')
    objects = NetManager()

Admin form displays both Lan and Server host in bold letters.

When I voluntarily forget to supply a LAN object, Admin form displays a red 'This field is required' error message above LAN selection menu. This is what I expected.

When I voluntarily forget to supply a Server Host object, Django throws a RelatedObjectDoesNotExist exception. This is NOT what I expected as I expected it to display a red 'This field is required' error message.

1. Are my expectations correct ?
2. How can I debug or work around this ? (adding a specific rule in model's clean function was not effective as I think this function is called after individuals fields are checked)

Best regards

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/474ea0a5-d39a-47f5-aa2a-2524bd294f07n%40googlegroups.com.

No comments:

Post a Comment