14π
Your middleware suggestion got me thinking, and I now think the best idea is to overwrite the standard AuthenticationMiddleware
. That class assigns a LazyUser
object to the request, which is resolved to the correct user, when accessed, by calling contrib.auth.get_user
. This is probably the right place to override things, so that it calls your customised get_user
function which returns your subclassed AnonymousUser
.
5π
A simpler and more general (but less safe) solution would be to just replace django.contrib.auth.models.AnonymousUser
with your own class:
class YourAnonymousUser(...):
...
import django.contrib.auth.models as django_auth_models
django_auth_models.AnonymousUser = YourAnonymousUser
As of 1.10.5, Django lazily imports the anonymous user class, so you wonβt run into issues with core Django. You also rarely interact with AnonymousUser
directly, since you could just use .is_anonymous()
, so you should be fine as long as you know how your dependencies use AnonymousUser
.
- Python-social-auth with Django: ImportError: No module named 'social_django'
- The default "delete selected" admin action in Django
- No module named backends.default.urls
- AssertionError at /posts/ 'PostList' should either include a `queryset` attribute, or override the `get_queryset()` method
- Getting 415 code with django.test.Client's patch method
2π
The best way is to use a python package (or write your own) which will swap Django default AnonymousUser class with your own class. You can implement a middleware for this (I think itβs the best place).
For example, you can use django-custom-anonymous to provide customization of AnonymousUser. Also you can find it on pypi.
- Django forms give: Select a valid choice. That choice is not one of the available choices
- How can i get all models in django 1.8
- Django custom user model in admin, relation "auth_user" does not exist
- Django Test Client post() returns 302 despite error on view's post()
- Python/Django polling of database has memory leak
1π
Iβm starting to think a middleware is probably the easiest solution that checks the request.user
class and if is AnonymousUser then replaces it with a subclassed AnonymousUser that has the extra properties.
That sound reasonable?
- How to read sql query to pandas dataframe / python / django
- What is the IP address of my heroku application
- AuthAlreadyAssociated Exception in Django Social Auth
0π
You could probably use django-rest-framework which allows you to customise AnonymousUser through UNAUTHENTICATED_USER
in settings likes this
REST_FRAMEWORK = {
'UNAUTHENTICATED_USER': 'application.AnonymousUser',
}
Keep in mind that this user class will only be available from DRF views
- Why can't I upload jpg files to my Django app via admin/?
- How can I easily convert a Django app from mySQL to PostgreSQL?
-1π
You should subclass or create a model that has a one to one relationship with the AnonymousUser class.
- Django β filtering by "certain value or None"
- Enable oauth login with django-allauth but a custom provider
- Returning CSV format from django-rest-framework?
- Where has 'django.core.context_processors.request' gone in Django 1.10?
- How to have a "random" order on a set of objects with paging in Django?