I'm working on a Django
project. I have two types of members (user profiles) - Customer and Translator. I would like to see those profiles in a Django Admin including it's User attributes like username
, surename
etc.
Now, when I create let's say UserTranslatorProfile called Thomas
, I can see his name, username etc. in Users
in Auth
in Django Admin
and the other attributes which belongs to it's profile in UserTranslatorProfiles
which is very uncomfortable. (In UserTranslatorProfile
I can see which user it is but I can't see his attributes and can't change them).
The two profiles are here:
class UserTranslatorProfile(models.Model):
user = models.OneToOneField(User)
languages = models.ManyToManyField(Language)
class UserCustomerProfile(models.Model):
user = models.OneToOneField(User)
And they are of course registered in admin.py
So the question is: Is it possible and if, how to see all User
and UserProfile
attibutes by clicking on UserProfile
in Django-Admin
?
Add a ModelAdmin
for UserTranslatorProfiles
and add the username
, email
and other related fields into the fields
attribute.
ModelAdmin
are put in the admin.py
file and they further customize the way models are displayed in the admin.
from django.contrib import admin
from myproject.myapp.models import UserTranslatorProfiles # Import your model
class UserTranslatorProfilesAdmin(admin.ModelAdmin):
fields = ('username', 'email, ) # Enter full list of fields here
admin.site.register(UserTranslatorProfiles, UserTranslatorProfilesAdmin)
Just need to set ModelAdmin and register it with admin site
step 1:
create a admin.py in your application if not exist.
step-2:
import your model you want to show on admin site.
step-3:
Create a admin model and register it with admin site.
from django.contrib import admin
from myproject.myapp.models import Author #import your model
class AuthorAdmin(admin.ModelAdmin):
fields = ('name', 'title', 'view_birth_date')# define-fields
admin.site.register(Author, AuthorAdmin) #now register your custom-admin model
for more
https://docs.djangoproject.com/en/1.9/ref/contrib/admin/#modeladmin-objects
Unfortunately this is more difficult than it needs to be/should be. Without an inline admin, the admin add/change forms are only built to edit one model.
If you're able to change your user model, you can accomplish this by creating your own custom User object that inherits from User. Then, in settings.py, set AUTH_USER_MODEL to your new model. Note that this should be done before data has been input, and your first migrations have been done (so you'd need to start fresh.)
If you're unable to do so, I can think of two other directions you could take:
Easier: Create an inline admin for the User object, and register that to your translation object. Then you'll have a single row in an inline admin at the bottom of the page, which can be used to edit the User. I hate this from an end-user UX perspective, but it could get the job done.
Neater: Create a custom form, add fields that correspond to those that you'd like to edit in the User model, and then in the form's clean and save methods, validate/save the methods of the underlying User object. For example, create the form fields "tra_username", "tra_firstname", "tra_password"... Then, in the form's save method (after validation), do this for each field
UserCustomProfile.user.first_name = form.cleaned_data.get("tra_firstname")`
UserCustomProfile.user.save()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.