Kani999 / netbox-attachments

Plugin to manage attachments for any model
Apache License 2.0
55 stars 4 forks source link

Stale attachment entries causes exception #38

Closed whtkam closed 1 year ago

whtkam commented 1 year ago

When accessing the plugin -> attachments, the following exceptions occur. I assume this has something to do with the stale entries in the database. (I can't get rid of the attachment from deleted references.) https://github.com/Kani999/netbox-attachments/issues/37

<class 'django.core.exceptions.FieldError'> Field 'parent' does not generate an automatic reverse relation and therefore cannot be used for reverse querying. If it is a GenericForeignKey, consider adding a GenericRelation.

Python version: 3.9.11 NetBox version: 3.5.8 Plugins: netbox_attachments: 2.0.1 netbox_bgp: 0.10.2 netbox_documents: 0.6.0 netbox_ipcalculator: 1.1 netbox_topology_views: 3.6.2

Kani999 commented 1 year ago

I'll take a look at it during next week.

Kani999 commented 1 year ago

@whtkam OK, I'm able to reproduce the problem.

Deleting the parent object causes a problem wich prevents the deletion or edition of the attachment object.

The problem is that the parent objects do not have the GenericRelation to attachments like GenericRelation(to='plugins.netbox_attachments.NetBoxAttachmet') or something like that in the model. This cannot be added to a model because with the plugin you can't edit the core models.

I have to look deeply at this problem and solve it otherwise. Something like bypassing the method for edit/delete. When editing/deleting, take a look at the parent. If the parent does not exist, clear the "contant_type" and "object_id".

Kani999 commented 1 year ago

So I found a solution when you delete a parent object it will also delete the attachment.

Now I'm thinking about another problem. If you add an attachment to a model from another plugin. Then you uninstall the plugin. Now the parent objects of attachment are missing, so they should be removed.

I think that creating a script to remove stale attachments should be fine.

Kani999 commented 1 year ago

Maybe I'm overthinking the solution for the "Removing the plugin" problem. Enabling to remove attachments from the list view would be enough I suppose.