Can django’s auth_user.username be varchar(75)? How could that be done?

There’s a way to achieve that without touching the core model, and without inheritance, but it’s definitely hackish and I would use it with extra care. If you look at Django’s doc on signals, you’ll see there’s one called class_prepared, which is basically sent once any actual model class has been created by the metaclass. … Read more

Django: Populate user ID when saving a model

UPDATE 2020-01-02 ⚠ The following answer was never updated to the latest Python and Django versions. Since writing this a few years ago packages have been released to solve this problem. Nowadays I highly recommend using django-crum which implements the same technique but has tests and is updated regularly: https://pypi.org/project/django-crum/ The least obstrusive way is … Read more

How to use permission_required decorators on django class-based views

There are a few strategies listed in the CBV docs: Decorate the view when you instantiate it in your urls.py (docs) urlpatterns = [ path(‘view/’,login_required(ViewSpaceIndex.as_view(..)), … ] The decorator is applied on a per-instance basis, so you can add it or remove it in different urls.py routes as needed. Decorate your class so every instance … Read more

Extending the User model with custom fields in Django

The least painful and indeed Django-recommended way of doing this is through a OneToOneField(User) property. Extending the existing User model … If you wish to store information related to User, you can use a one-to-one relationship to a model containing the fields for additional information. This one-to-one model is often called a profile model, as … Read more