When using django mailer (https://github.com/pinax/django-mailer) I realized that the default tables added to the admin area (such as Message logs and Messages) did not add the message_log field which indeed is available if one looks at the tables that are added.
Since the error message is very valuable to me I wanted to add it, and simply added the "log_message" to the app's MessageLogAdmin like this:
class MessageLogAdmin(MessageAdminMixin, admin.ModelAdmin):
list_display = ["id", show_to, "subject", "message_id", "when_attempted", "result", "log_message"]
list_filter = ["result"]
date_hierarchy = "when_attempted"
readonly_fields = ['plain_text_body', 'message_id']
search_fields = ['message_id']
However, is there really no other way to customize the admin area for django-mailer other than modifying the source code? E.g through settings.py
No you can't do that via settings.py
If I understand correctly, you don't want to fork the app just to edit admin.py, but rather keep it in the requirements.txt file. In that case you could do something like:
class MyOwnMessageLogAdmin(MessageAdminMixin, admin.ModelAdmin):
list_display = ["id", show_to, "subject", "message_id", "when_attempted", "result", "log_message"]
list_filter = ["result"]
date_hierarchy = "when_attempted"
readonly_fields = ['plain_text_body', 'message_id']
search_fields = ['message_id']
admin.site.unregister(MessageLog)
admin.site.register(MessageLog, MyOwnMessageLogAdmin)
Related
The invoice_date field on one of my models is not showing up in my admin.
invoice_date = models.DateField(auto_now=True, auto_now_add=False, null=True, blank=True)
I've tried the following:
renaming the field
running makemigrations/migrations
restarting the server
deleting cookies
I can successfully write to the field and pull data from it, so it appears to be there and functioning as desired. I just can't see it in the admin.
I have no special code in my admin.py. I've just registered the model
admin.py
from userorders.models import UserCartItem
admin.site.register(UserCart)
Any suggestions are welcome! Thanks!
Accoring to Django's documentation:
As currently implemented, setting auto_now or auto_now_add to True will cause the field to have editable=False and blank=True set.
You can circumvent this by explicitly defining it on the ModelAdmin class:
from userorders.models import UserCartItem
class UserCartItemAdmin(admin.ModelAdmin):
list_display = ['invoice_date']
fields = ['invoice_date']
# if you want the field just to visible but not editable
# readonly_fields = ['invoice_date']
admin.site.register(UserCartItem, UserCartItemAdmin)
You could try something following-
from userorders.models import UserCartItem
class UserCartItemAdmin(admin.ModelAdmin):
list_display = ['field_name_1', 'field_name_2', 'invoice_date']
admin.site.register(UserCartItem, UserCartItemAdmin)
I have the following code in my admin.py:
class UserManagedGroupAdmin(admin.ModelAdmin):
inlines = [MembershipInline]
search_fields = ('name', 'leader__username', )
list_display = ('__unicode__', 'leader', )
filter_horizontal = ('permissions', )
raw_id_fields = ('leader', )
admin.site.register(UserManagedGroup, UserManagedGroupAdmin)
The magnifying glass icon for searching doesn't appear in the admin page.
This is what I'm getting:
As you can see it's showing the unicode method of the model instead of the search icon I want.
Field 'leader' is a ForeignKey to User.
Could it be that django disables the search for ForeignKeys to User for security reasons, or am I doing something wrong?
The widget would be perfect for choosing users... I mean, I can't leave a huge select there with every user of my site.
Thanks.
I've found the problem thanks to this message in django-users.
I had to register in the admin the model to which the ForeignKey points to.
The search doesn't work without that.
Hi encounter the same issue but reason's a bit different.
To integrate the User and UserGroup with another app's admin (e.g. some_app)
I added below code to some_app/admin.py
class ProxyUser(User):
class Meta:
proxy = True
verbose_name = User._meta.verbose_name
verbose_name_plural = User._meta.verbose_name_plural
class ProxyGroup(Group):
class Meta:
proxy = True
verbose_name = Group._meta.verbose_name
verbose_name_plural = Group._meta.verbose_name_plural
admin.site.unregister(Group)
admin.site.unregister(User)
admin.site.register(ProxyGroup)
admin.site.register(ProxyUser, UserAdmin)
I think the unregister(...) will affect the other app's admin Globally!
That's another cause of missing search icon.
In the admin.py file add:
admin.site.register(YourModel)
This did the trick, Where YourModel is the model to be displayed with the magnifying glass
I have an image field in my models.py...
qr_image = models.ImageField(
upload_to="public/uploads/",
height_field="qr_image_height",
width_field="qr_image_width",
null=True,
blank=True,
editable=False
)
When it's editable=False I get a nasty error when trying show it. I don't want the field editable, however I do want the image to show in admin 'edit' page i.e. fieldsets
I'm new to Django, can someone tell me if this is possible and point me in the right direction here?
thank you.
If you have a ModelAdmin like this:
class MyModelAdmin(admin.ModelAdmin):
fieldsets = [
('Fieldset', {'fields': ['model_field', 'another_field', 'readonly_field']}),
]
where readonly_field has editable=False or if it is a DateTimeField with auto_now_add=True then visiting the admin page for your model will result in a field not found error.
To include a readonly field in your fieldsets, include readonly_fields like this:
class MyModelAdmin(admin.ModelAdmin):
fieldsets = [
('Fieldset', {'fields': ['model_field', 'another_field', 'readonly_field']}),
]
readonly_fields = ('readonly_field',)
An easy way to accomplish this is to make the field read-only in the ModelAdmin: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.readonly_fields
I'm using django.contrib.comments for allowing users to comment on a blog.
How is it possible to make the comments display on the Django Admin /admin/comments/comment/ and make them clickable for editing?
[Here should be an image, but since this is my first question and I have no credit, it is not allowed to include images]
The comments can be accessed via /admin/comments/comment/comment_id/ and edited without problems.
Any ideas how to get that solved?
Looking at django.contrib.comments.admin, it should be already visible in your admin panel, provided you added 'django.contrib.comments' to INSTALLED_APPS.
EDIT:
Second look at admin.py from Comments app revelaed that CommentsAdmin.list_display doesn't contain the comment itself. So I would either inherit from that CommentsAdmin, override list_display and then unregister and re-register Comment with MyNewCommentsAdmin - or I would just monkey-patch CommentsAdmin. Whichever works.
Thank you Tomasz,
The problem was 'content_type' in list_display, which resulted in nothing at all displayed. Removing it from MyCommentsAdmin resolved the problem:
app/admin.py:
class MyCommentsAdmin(admin.ModelAdmin):
fieldsets = (
(_('Content'),
{'fields': ('user', 'user_name', 'user_email', 'user_url', 'comment')}
),
(_('Metadata'),
{'fields': ('submit_date', 'ip_address', 'is_public', 'is_removed')}
),
)
list_display = ('name', 'ip_address', 'submit_date', 'is_public', 'is_removed')
list_filter = ('submit_date', 'site', 'is_public', 'is_removed')
date_hierarchy = 'submit_date'
ordering = ('-submit_date',)
raw_id_fields = ('user',)
search_fields = ('comment', 'user__username', 'user_name', 'user_email', 'user_url', 'ip_address')
admin.site.unregister(Comment)
admin.site.register(Comment, MyCommentsAdmin)
urls.py:
from django.contrib import admin
admin.autodiscover()
import app.admin
add to answer Meilo:
if you use standard comment's framework (like: #in url.py
url(r'^comments/', include('django.contrib.comments.urls')),
you wish overwrite behavior comments model, you need import
#apps.admin.py
from django.contrib.comments.models import Comment
I have a class Task(models.Model), and i didn't define id field explicitly (since it defines automatically for you). I checked in the database, it exists for the Task. Now i would like to display it in the list via list_display property in admin.ModelAdmin. I have a bunch of things in there, only id is not showing up for any of the rows i have. Everything else works fine. Anyone know anything special i have to do to get id to display?
EDIT:
if i define a function as follows:
def ID(self, obj):
return obj.id
and i put this function in list_display, it will display id just fine for some reason.
Thanks a lot!
Jason
It doesn't show by default. You need to create a custom Admin class for that model and then add 'id' to the list_display value. E.g. in whatever/admin.py:
class TaskAdmin(admin.ModelAdmin):
list_display = ['id', 'name', etc. etc]
admin.site.register(Task, TaskAdmin)
See the docs for more details.
I believe you have also used list_editable in your admin.ModelAdmin. This causes the ID to be hidden and is a known bug: http://code.djangoproject.com/ticket/12475.
The work around is to state list_display_links option:
class AdAdmin(admin.ModelAdmin):
list_display = ['id', 'ad_title', 'status', 'sponsor' ... ]
list_display_links = ['id', 'ad_title'] # forcing to display id of model
list_editable = ['status']
...
Thanks and hope this helps.