So i work in a company and we constantly need to add view fields for the user, I would like to know if there is a way to make this option available to the end user, for example a dropdown with the options that the model admin where he is allowing.
Today we use the django way of doing admin, for example:
list_display = (
'course',
'class',
'discipline',
'teacher',
'start_date',
'end_date'
)
I don't know if this what you are looking for, but give a try: How can I dynamically specify the "list_display" attribute of a django ModelAdmin class?
A kind regard
Emilio
Related
I have a User model that has these two fields:
permissions = models.ManyToManyField(Permission)
groups = models.ManyToManyField(Group)
I register the model in the admin. When I view the user model that I made in the admin section I get a multiple select menu that looks like this.
I would much prefer the much better-looking menu like the one that is in the auth user model that comes built into Django admin (looks like this)
Any ideas on what I need to do to be able to access this sort of select menu?
Thanks for your help.
Django newb from PHP (finally)
Use filter_horizontal (or filter_vertical) field in your admin class.
For example:
#admin.register(User)
class UserAdmin(admin.ModelAdmin):
...
filter_horizontal = 'groups', 'permissions', 'any_other_m2m_field'
...
Also link to Django docs:
https://docs.djangoproject.com/en/3.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.filter_horizontal
If you are on the django admin page for the model Group. You don't know that there is a reverse relation to user.
Some people (not me) have difficulties with it.
Is there a way to show all reverse relations, so that you can jump to the matching admin pages?
Example:
On admin page for Group I want a link to User (and all other models which refer to it).
This should happen by code, not by hand with templates.
This method doesn't automatically add links to all related models of a Group, but does for all Users related to a Group (so for one related model at a time). With this you'll get an inline view in your Group with the related Users.
You could probably extend this technique to make it automatically work for all related fields.
class UserInline(admin.StackedInline):
model = User
extra = 0
readonly_fields = ('change',)
def change(self, instance):
if instance.id:
# Django's admin URLs are automatically constructed
# based on your Django app and model's name.
change_url = urlresolvers.reverse(
'admin:djangoapp_usermodel_change', args=(instance.id,)
)
return '<a class="changelink" href="{}">Change</a>'.format(change_url)
else:
return 'Save the group first before editing the user.'
change.allow_tags = True
class GroupAdmin(admin.ModelAdmin):
list_display = ('name',)
inlines = (UserInline,)
You might also be interested in this extension I created for Django admin pages to link to related objects:
https://github.com/gitaarik/django-admin-relation-links
It's quite easy to use and makes the admin a lot more convenient to use :).
I've got multiple instances of a model, and each instance has a related email address. However, several instances have the same connected email address but when I put filter['email'] into my admin.py, I get a long list of the instances' emails, i.e. multiple copies of the same email in several cases.
Is there a way I can remove emails being listed multiple times? Or a way of customising the filter view into something a little nicer? (drop down menu maybe?)
I don't have a ManyToManyField relationship currently, or anything like that. I just have instances in my database with the fields name and email. My models.py looks like this:
import ldapdb.models
from ldapdb.models.fields import CharField, IntegerField, ListField
class Item(ldapdb.models.Model):
item = CharField(db_column='item', max_length=30, primary_key=True, unique=True)
email = CharField(db_column='mail', max_length=20)
My admin.py looks like so:
from items.models import Item
from django.contrib import admin
class ItemAdmin(admin.ModelAdmin):
readonly_fields = ('email',)
list_display = ('item', 'email')
list_filter = ['email']
search_fields = ['item']
admin.site.register(Item, ItemAdmin)
Obviously I've been looking at https://docs.djangoproject.com/en/1.3/ref/contrib/admin/ but I can't really see much by the way of customising my admin's filter view.
Can you post some of your code? I'm not entirely sure I understood the relationship between the instances to your email - is it an email field? a ForeighKey to a different model? how is there more than one if it's not a ManyToMany or similar relationship? And how is the filtering done in the admin?
EDIT
Ok now I understand the problem. What you want is not possible. See for the django admin site the fact that they are the same email doesn't matter because it's still a different object. There's no way around that without either specifying that field to be unique or messing with the admin site code.
A better solution would be to configure the email as searchable in the admin model and then when you search for email example#example.com it would bring all matches back.
Another good solution is to make email a different model and link it to the Item model through a ManyToMany relationship. Then you create an EmailAdmin with a method that shows you all related items for each email.
It all depends on what you actually need. Ultimately you might want to write your own view or mess around with the admin site to modify it to what you need.
Also, you might want to change the email from CharField to EmailField. Hope this helps!
I'm trying to understand the django admin better and at the same time, I'm trying to add one more field to the current user admin. In models.py I've done
User.add_to_class('new_field', models.BooleanField(default=False))
and in admin.py I've got the following (with fieldsets basically just copied from django/contrib/auth/admin.py)
class AdjUserAdmin(UserAdmin):
list_display = UserAdmin.list_display + ('new_field',)
list_filter = UserAdmin.list_filter + ('new_field',)
fieldsets = UserAdmin.fieldsets
fieldsets[1][1]['fields'] = ('first_name','last_name','email','new_field')
The problem is, when I do this I get the error:
AdjUserAdmin.fieldsets[4][1]['fields']' refers to field 'new_field' that is missing from the form.
I've looked at UserChangeForm, but it looks like it's already correctly pulling in User as the model. I'm not sure as to why new_field is missing from the form.
Thanks
In regards to this being smelly code
I know this is a smelly monkey patching way to go about doing this, but subclassing gives me issues mainly for these reasons.. if I could get it to work the way stated above, I'd be happy.. and maybe smelly.
In regards to the recommended way
I'm aware of the recommended way of creating a user profile, just that in particular situations, I don't see the merit in creating an entire new table and having an additional call to the database when all I want to store is an extra bit of information such as is_private or some such. If I'm storing lots more info, then I agree, setting up a user profile is preferable.
First the "is it plugged in?" question -- Have you manually added new_field to the users table in the database? Syncdb wouldn't have taken care of that, of course.
After that, I would try appending the fields onto the existing UserAdmin rather than rebuilding it from scratch:
from django.contrib.auth.admin import UserAdmin
UserAdmin.list_display += ('new_field',) # don't forget the commas
UserAdmin.list_filter += ('new_field',)
UserAdmin.fieldsets += ('new_field',)
Just figured this out, perhaps this could help you as well.
Since you don't mention that you have a separate profile, if you want to simply add a column to the existing User admin, you can do the following in admin.py:
First you create a custom admin by subclassing UserAdmin:
class CustomUserAdmin(UserAdmin):
list_display = UserAdmin.list_display + ('is_complete',)
def is_complete(self, obj):
# Example here, you can use any expression.
return SomeOtherClass.objects.get(my_field=obj).is_complete()
# Not required, but this gives you a nice boolean field:
is_complete.boolean = True
Then unregister the existing UserAdmin and register your own:
admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)
It is preferred to write your own user profile class, and attach it to the User model. Then you can use the get_profile() method to retrieve the profile from the user.
Subclassing the profile admin from an Inline Admin should also allow you to edit the profile on the user's page, which is almost what you're trying to do.
This post has a really good write-up on the issue: http://www.b-list.org/weblog/2006/jun/06/django-tips-extending-user-model/
Despite your DB look up concerns for having a UserProfile, that is really the way you should go for this problem. There are two main reasons for this:
The User Model is pretty tightly coupled to Django's authentication system and to many other plugins. Monkey patching it isn't a good idea.
Django has built-in support for creating User Profiles and many extensions know how to play nicely with these. By using User Profiles (see http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users for the main discussion on this) you can benefit from that.
If you're looking to add functionality to the Admin interface for users, then this note may be helpful:
http://www.thenestedfloat.com/articles/displaying-custom-user-profile-fields-in-djangos-admin/index.html
Regards,
Ian
Based on #anschauung response, what worked for me in Django 3.2 is:
from django.contrib.auth.admin import UserAdmin
# don't forget the commas
UserAdmin.list_filter += ('new_field',)
UserAdmin.fieldsets += (('Extra Fields', {'fields': ('new_field', )}),)
I got the 'that is missing from the form' error, and discovered that it was due to my field being marked as 'editable=False' in the model.
in django admin the views that show the register's just have
a link to "edit", but what happen if a need an extra(S) links to
another views?
for example:
i have view that show the list of registered People, the nick is
linking to the Edit page (the normal way of Django), but i need
another links that will show me the "articles" of the people and
another the "comments" of the people.
how ill make this with django admin?
Thanks
(I'm assuming some field names from your models to answer)
Make the author field from "comment" searchable:
class CommentAdmin(admin.ModelAdmin):
search_fields = ("=author",)
Use list_display and HTML to control what's displayed on the people's list admin page:
def comments(obj):
return ('comments' % obj.name)
comments.short_description = 'comments'
comments.allow_tags = True
class PeopleAdmin(admin.ModelAdmin):
list_display = ("name", comments,)
And change /admin/pathto/comments/ to whatever your comment's admin list page is.
Basically you're going to direct your users to the comments search result page.