what is BaseUser in django . why is it used?
What role does it play during the creation of custom user model and why attributes it present ?
BaseUser is base class for every Django user class. Also, BaseUerAdmin is inherited from BaseUser.
Related
I want to have a custom tab for staff users so in admin.py I do:
class StaffUser(User):
class Meta:
proxy = True
#admin.register(StaffUser)
class AdminUserAdmin(admin.ModelAdmin):
pass
But on the admin site, whenever I add a new user with this interface I can't log in with it since for some reason it sets it's password as plain text instead of hashing it.
I've read this post BUT if I do that and change StaffUser to inherint from AdminUserI get this other error
AttributeError: type object 'StaffUser' has no attribute '_meta'
You should declare your models in models.py (or a models package), not admin.py.
UserAdmin (there is no such thing as an AdminUser in the Django packages) is a subclass of ModelAdmin, that is, a class for registering models in the Django admin. It is not a model subclass that you can extend to make new models.
Also, if you are trying to extend the User just to distinguish users with access to the Django admin site, note that there is already an is_staff property in the default User model. And in any case, if you still want to extend the User model, you should be extending AbstractUser instead as stated in the docs.
Since Django recommends having a custom user model for a new project, I've extended the AbstractUser model without adding any additional fields, but how do I create a signup page for new users with my new user model?
My model:
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
pass
After creating customer model you have to build registration form.
you can check out this video he explained it well.
Also you can add additional fields
AbstractBaseUser and UserCreationForm
I have created a custom user model before making any migration and I wanted to move it from the app panel to the auth panel in the admin page.
To do that I created a proxy user model:
class User(AbstractUser):
pass
class ProxyUser(User):
pass
class Meta:
app_label = 'auth'
proxy = True
and then in admin.py:
from django.contrib.auth.admin import UserAdmin
from .models import ProxyUser
admin.site.register(ProxyUser, UserAdmin)
The problem is that the auth_permission table has permissions for user and proxyuser.
Can't understand why if I'm using a proxy and only one user table was created the permissions table behaves as if there were two (proxyuser and user).
Am I missing something?
Thanks in advance
Django uses the content type framework to keep track of "permissions" for various models. Proxy models get their own permissions.
This is explained in the authentication section of Django docs:
Proxy models work exactly the same way as concrete models. Permissions are created using the own content type of the proxy model. Proxy models don’t inherit the permissions of the concrete model they subclass
I feel what you're trying to achieve with the proxy model is unnecessary. I personally wouldn't worry much about 'Users' appearing under a separate section in the Django Admin. You will instead add unnecessary complexity to the code by using a proxy model (A future developer/you would wonder wether to use the custom User class or the ProxyUser class).
You may have done all migrations and no only for your apps, if you don't specify the app to migrate, Django makes all of the migrations. On the other way, maybe you can't log into the admin site if you doesn't do it.
I'm trying to use a simple abstract base class in django's admin interface with neo4django.
Example models.py
from neo4django.db import models
class Parent(models.NodeModel):
name = models.StringProperty()
class Meta:
abstract = True
class Child(Parent):
pass
Example admin.py:
from neo4django import admin
from core.models import Child
class ChildAdmin(admin.ModelAdmin):
pass
admin.site.register(Child, ChildAdmin)
The 'name' field doesn't appear in the admin interface.
If I use the same basic structure, but with django.db instead of neo4django.db, it all works fine. Anyone spot where I've gone wrong?
Update from comments:
This has been tried with django 1.5.5 and 1.5.4
The neo4django version is from the github repo
Registering the model with or without a ModelAdmin have both been tried and made no difference
Have you tried just registering the model, without the ModelAdmin?
I am using mongoengine with django. I have a CustomUser model inheriting from mongoengine.django.auth.User that defines some fields. I have a field, which is only needed for some users. I don't want this field in every User objects. As mongoengine.django.auth.User is inherited from mongoengine.Document from which CustomUser model is inherited I can't add fields into it dynamically.So I made my CustomUser model to inherit from both mongoengine.django.auth.User and mongoengine.DynamicDocument
from mongoengine.django.auth import User
from mongoengine import DynamicDocument
class CustomUser(User, DynamicDocument):
# fields
Using this method I am able to dynamically create fields for CustomUser. But I want to know if it is ok to do this. If there are any other better methods available, please suggest.
Thanks.
Yes thats totally valid as the mongoengine.django.auth.User class is inheritable.
Alternative approaches might be to extend explicitly for different types of user eg:
class AdminUser(User):
role = StringField()
Then you can just use:
User.objects(username=blah)
And if that User is an AdminUser it will return the correct class instance