Reputation: 441
I am new to django. I have the following codes:
model:
class MyUser(AbstractUser):
profile = models.OneToOneField(Profile, null=True, on_delete=models.PROTECT)
My profile model contains only some personal fields like full_name, gender, birthdate, etc.
admin:
class MyUserInline(admin.StackedInline):
model = MyUser
exclude = ('first_name', 'last_name', 'username', 'plan', 'password')
fieldsets = (
(_('Personal info'), {'fields': ('email',)}),
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
'groups', 'user_permissions')}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
)
class MyUserChangeForm(UserChangeForm):
class Meta(UserChangeForm.Meta):
model = MyUser
def clean_password(self):
return ""
class ProfileAdmin(admin.ModelAdmin):
inlines = (MyUserInline,)
change_user_password_template = None
form = MyUserChangeForm
change_password_form = AdminPasswordChangeForm
ordering = ('myuser__email',)
def save_model(self, request, obj, form, change):
obj.myuser.username = obj.myuser.email
obj.save()
list_display = ('full_name', 'myuser_email', 'myuser_is_staff', 'myuser_is_superuser', 'myuser_is_active')
def user_change_password(self, request, id, form_url=''):
if not self.has_change_permission(request):
raise PermissionDenied
user = self.get_object(request, unquote(id))
if user is None:
raise Http404(_('%(name)s object with primary key %(key)r does not exist.') % {
'name': force_text(self.model._meta.verbose_name),
'key': escape(id),
})
if request.method == 'POST':
form = self.change_password_form(user, request.POST)
if form.is_valid():
form.save()
# rest of method
# other minor methods
The problems are:
In recent actions or after editing a user, I see Profile Object instead of username or email. enter image description here
see here: my other problem
In profile edit page, the permission part contains only a box containing a list of all possible permissions, however there must be another box containing user current permissions list
What it looks like now: enter image description here
What it must look like: enter image description here
tnx
Update
for my second problem (change password), I added following lines to Profile
model:
def set_password(self, raw_password):
self.myuser.password = make_password(raw_password)
self.myuser._password = raw_password
while debugging, raw_password
is correctly what I have inserted in the form, and after moving from first line, self.myuser.password
has new value, but the new value is not submitted into DB.
Upvotes: 1
Views: 1696
Reputation: 1984
Point 1
Define a __unicode__
or __str__
(according to your python version) method in your Profile
model that returns what you want to display.
Point 3
You can add the following line to your ProfileAdmin
:
filter_horizontal = ['user_permissions']
to produce a nice two columns filtering field as displayed in your image. See docs.
You can also directly use the widget FilteredSelectMultiple
from django.contrib.admin.widgets
Upvotes: 2