Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix relationship issues with missing models for defined contenttypes #1121

Merged
merged 4 commits into from
Dec 3, 2021

Conversation

glennmatthews
Copy link
Contributor

Fixes: #N/A

Fix a user-reported issue, the symptom being errors like the following when trying to edit a model instance:

<class 'AttributeError'>

'NoneType' object has no attribute 'objects'

Python version: 3.8.12
Nautobot version: 1.2.0b1

The issue is that if we define a Relationship between core models and models from a plugin, then subsequently disable or uninstall the plugin, then the ContentTypes for the plugin still exist in the DB (and hence the Relationship still references them) but Nautobot doesn't know about the actual data models associated with those ContentTypes. Thus, content_type.model_class() returns None rather than a model class as expected. We didn't have logic in the Relationships code to handle the case where model_class is None, resulting in the above error symptom.

Rather than adding a signal or startup code to automatically delete Relationships that reference ContentTypes with no associated model classes, which could be problematic if for instance a plugin is only temporarily disabled to troubleshoot some other issue, I've just added error handling logic for the various cases I was able to reproduce with the above trigger:

  • When editing a core model that has Relationships that reference a model-less ContentType, instead of providing a DynamicModel[Multiple]ChoiceField for the relationship, provide a simple MultipleChoiceField with no selectable options. (I wasn't able to figure out a straightforward way to omit the relationship from the edit form entirely in this case)
  • When displaying a detail view of a core model that has such Relationships, if the relationship doesn't explicitly provide a label to use for the target objects, use the name of the relationship as the fallback label (instead of trying to use the target object model class's name, which we don't have).
  • When cleaning a Relationship edit form, if it references a content type that doesn't have an associated model class, raise a ValidationError. (This shouldn't be possible to trigger via normal workflows, since the content-type dropdowns are populated based on the available model classes, but I include it for sake of completeness.)

@glennmatthews glennmatthews merged commit 129941c into develop Dec 3, 2021
@glennmatthews glennmatthews deleted the gfm-relationship-issue branch December 3, 2021 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants