netzkolchose / django-computedfields

Provides autogenerated autoupdated database fields for model methods.
MIT License
94 stars 14 forks source link

How to add ComputedFieldsModel to Admin site #127

Closed biostu24 closed 1 year ago

biostu24 commented 1 year ago

I tried to add a ComputedFieldsModel to the admin site using ComputedModelsAdmin to show the computed field, however, I can't figure out how to use this class as I get an error. It would be helpful if usage instructions were added to the documentation.

This is how I tried to set up the admin.py file:

from django.contrib import admin
from computedfields.admin import ComputedModelsAdmin
from . import models as m

@admin.register(m.StandardModel)
class GenericAdmin(admin.ModelAdmin): 
    pass

# This does not work.
@admin.register(m.ComputedModel)
class ComputedAdmin(ComputedModelsAdmin): 
    pass

In the example above, I can see my ComputedFieldsModel on the summary of the admin site but when clicking on the model, I get 'ComputedModel' object has no attribute 'app_label'. I tried adding an app_label attribute to my model but this did not work. I believe I'm not using the ComputedAdmin class correctly. Any help would be greatly appreciated.

jerch commented 1 year ago

Ah this is a misunderstanding - ComputedModelsAdmin is not a special admin view class for computed models, but a helper admin class to get introspection into defined computed fields and their deps during development. You can activate the admin helper classes by setting COMPUTEDFIELDS_ADMIN = True in your settings.py.

Computed models have no dedicated admin view class, as I saw no need for this - in the end computed fields are just readonly fields on the model, so you can add/view them in admin as any other readonly field.

biostu24 commented 1 year ago

Thanks very much for the clarification. COMPUTEDFIELDS_ADMIN = True is what I am looking for.