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

Admin Model Not Integrating into Django Admin Page #38

Closed
ScottHull opened this issue Jun 2, 2019 · 7 comments · Fixed by #39
Closed

Admin Model Not Integrating into Django Admin Page #38

ScottHull opened this issue Jun 2, 2019 · 7 comments · Fixed by #39
Assignees
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@ScottHull
Copy link

Went to add this app to my project and noticed it wasn't automatically added to my Django admin page. I tried manually adding it in my admin.py and it appeared:

from rest_framework_api_key.admin import APIKeyAdmin
from rest_framework_api_key.models import APIKey

admin.site.register(APIKey, APIKeyAdmin)

However, when I navigated to Home/API Key Permissions/API Keys I received the following error:

AttributeError at /admin/rest_framework_api_key/apikey/ Unable to lookup 'has_expired_func' on APIKey or APIKeyAdmin

My temporary fix was to create a new admin model by subclassing the one given by this app:

from rest_framework_api_key.admin import APIKeyAdmin
from rest_framework_api_key.models import APIKey

class BaseAPIKeyAdmin(APIKeyAdmin):

    list_display = (
        "name",
        "prefix",
        "created",
        "expiry_date",
        "revoked",
    )

admin.site.register(APIKey, BaseAPIKeyAdmin)

Thereby removing has_expired_func from list_display.

@florimondmanca
Copy link
Owner

florimondmanca commented Jun 2, 2019

Hi Scott, thanks for reporting this. I did not get this error on my side. Also this is the first time someone has issues with the admin panel not showing up in the admin site. Can you share the Python, Django and DRF versions you are using?

@ScottHull
Copy link
Author

ScottHull commented Jun 2, 2019

Sure thing. Worth noting that I verified that this package was installed correctly (i.e. in INSTALLED_APPS) and that I had run the migrations.

Python==3.5.6
Django==2.1.5
djangorestframework==3.7.7

Edit: typo

@florimondmanca florimondmanca added bug Something isn't working good first issue Good for newcomers labels Jun 3, 2019
@florimondmanca
Copy link
Owner

So, I was able to reproduce the issue with the versions you listed. I just had to run system checks after creating a new project and adding the DRF and DRF-api-key apps:

$ ./manage.py check
SystemCheckError: System check identified some issues:

ERRORS:
<class 'rest_framework_api_key.admin.APIKeyAdmin'>: (admin.E108) The value of 'list_display[4]' refers to 'has_expired_func', which is not a callable, an attribute of 'APIKeyAdmin', or an attribute or method on 'rest_framework_api_key.APIKey'.

System check identified 1 issue (0 silenced).

Upgrading the Django and DRF versions to the latest available did not fix this either, so this is definitely a serious bug!

The following fixed the system checks:

  • Edit models.py, basically unwrapping the custom @djangoproperty:
    def _has_expired(self) -> bool:
        ...
    
    _has_expired.short_description = "Has expired"
    _has_expired.boolean = True
    has_expired = property(_has_expired)
  • Edit admin.py to use _has_expired.

If you're up for it, feel free to open a PR with these. :-)

@ScottHull
Copy link
Author

Excellent, thank you for following up with this in such a timely manner! Unfortunately I'm a bit busy to attempt a PR at the moment, but I'll keep it in mind if I get a moment.

My temporary fix that I listed in the first comment is working well for my project, especially since I made many custom modifications in the form of subclassing your models.

@florimondmanca
Copy link
Owner

florimondmanca commented Jun 3, 2019

Gotcha, I'll try and find some time to publish the fix later today then. :-) I suppose this is affecting all users of 1.2, so it's best if we get this out asap.

especially since I made many custom modifications in the form of subclassing your models.

We have an open issue to provide an abstract API key model to make it easier to customize API keys: #34. If that's something you'd be interested in, feel free to check it out and share your thoughts!

@Madril
Copy link

Madril commented Jun 3, 2019

Same is happening with Django 2.1.*, Python 3.7.3 and DRF 3.9

@florimondmanca florimondmanca self-assigned this Jun 3, 2019
This was referenced Jun 3, 2019
@florimondmanca
Copy link
Owner

Released in v1.2.1. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants