I have a Wagtail project that uses django-fluent-comments and I would like to be able moderate the comments in the admin pages. Based on this tutorial I added this to my wagtail_hooks.py file:
class CommentAdmin(ModelAdmin):
model = FluentComment
menu_label = 'Comments'
menu_icon = 'list-ul'
menu_order = 200
add_to_settings_menu = False
list_display = ('user', 'comment')
modeladmin_register(CommentAdmin)
But when I go to the Admin pages, there is no comments tab and no errors shown. I have tried to add a Pages model to my Admin page and that worked fine.
The FluentComment model is just:
class FluentComment(Comment):
"""
Proxy model to make sure that a ``select_related()`` is performed on the ``user`` field.
"""
objects = FluentCommentManager()
class Meta:
proxy = True
(In this file) So I wonder if I have the correct model. But the if I add print model.objects.all() to my CommentAdmin class it shows all my comments in the log.
Change FluentComment to django_comments.models.Comment. FluentComment is Proxy model which wagtail's modeladmin does not like.
Related
class TheList(admin.ModelAdmin):
list_display = ['name', 'age']
actions = None
I want to see a list in admin panel as readonly. If I use the below method it will show a list of objects which then need to be clicked on in order to display the name and age.
readonly_fields = ['name', 'age']
I have tried using the below admin functions which didn’t fix the problem:
has_delete_permission
has_add_permission
The list is still editable
I tried using this with list_display:
has_change_permission
that made the class not viewable in admin panel
I want it to be like list_display but readonly, is that even possible?
I want to display the ModelA as a TabularInline of ModelB and at the same time want to maintain the history of changes to ModelA using the Django simple history module.
Is there a way to do this? Went through the official documentation of both TabularInline and Django simpleHistory but couldn't get much out of it.
Model A
class ModelAInline(admin.TabularInline):
model = ModelA
def has_add_permission(self, request, obj=None):
return False
Model B
class ModelBAdmin(admin.ModelAdmin):
list_display = ("name",)
search_fields = ("name",)
readonly_fields = ("last_changed",)
inlines = (ModelAInline,)
How you display in the admin doesn't factor in to whether or not the history of changes is maintained. You can register ModelA as another admin view that inherits from SimpleHistoryAdmin so that you can view history on it, but as long as ModelA is registered with django-simple-history, the history of the model will be tracked.
I am fairly new to Django and can just about articulate what I am trying to do, so I apologise if the title isn't accurate.
I am using this survey app - https://github.com/Pierre-Sassoulas/django-survey
Within admin.py, I have the following two classes:
class SurveyAdmin(admin.ModelAdmin):
list_display = ('name', 'is_published', 'need_logged_user', 'template')
list_filter = ('is_published', 'need_logged_user')
inlines = [CategoryInline, QuestionInline]
actions = [make_published]
class ResponseAdmin(admin.ModelAdmin):
list_display = ('interview_uuid', 'survey', 'created', 'user')
list_filter = ('survey', 'created')
date_hierarchy = 'created'
inlines = [
AnswerTextInline, AnswerRadioInline, AnswerSelectInline,
AnswerSelectMultipleInline, AnswerIntegerInline
]
# specifies the order as well as which fields to act on
readonly_fields = (
'survey', 'created', 'updated', 'interview_uuid', 'user'
)
These create the pages for "Responses" and "Surveys" in the Admin page.
Admin area with Response and Survey links / their outputs
I can list the usernames of each user that submitted a Response to the Survey (using TabularInLine and model.response), but what I want to do is list all of the users that exist (so that I can add a timestamp of when they submitted their Response to the Survey/Filter by Response date/etc.).
What I've tried since is importing User from contrib.auth.models and creating a new UserAdmin class (so that I can at least list all users, and move on from there).
from django.contrib.auth.models import User
...
admin.site.unregister(User)
...
class UserAdmin(admin.ModelAdmin):
list_display = ('username',)
...
admin.site.register(User, UserAdmin)
I don't get any errors, but it's not displaying the UserAdmin class in the Admin page.
How can I list all of the registered users into the Admin page (in order to later add the timestamp of when a Response was submitted, if one was submitted)?
I'm using Django 1.11 if that's relevant.
I think you should have a list of all the user in the admin with the code you gave. Maybe you don't have any users in database ? I can't see the exact problem right now.
But without modifying the app, you should be able to see when a "Response" has been submitted or updated with the created or updated attribute of the Response class :
For example if you want to list the user and date of submission of a survey contained in the survey variable:
for response in survey.reponses:
print(response.user, "answered at", response.created)
If you want to loop on all users and see if the survey has been answered:
for user in User.objects.all():
response = Reponse.objects.filter(survey=survey, user=user)
if response:
print(user, "answered at", response.created)
else:
print(user, "never answered")
I hope this help.
I have registered some models to display in the admin area, but I would like for some fields to be hidden.
As an example, I have a TeachingClasses model with a BooleanField named 'Status' that is set to True or False depending if the class is open or not. But that is set somewhere else in the app. There is no need to have that field displayed in the admin area when someone wants to create a new class to attend.
As such, is there a way to hide that field in the admin area?
I have tried adding this to the app admin.py file but it did nothing
from django.contrib import admin
class MyModelAdmin(admin.ModelAdmin):
class TeachingClasses:
exclude = ('Status',)
but it's not working?
Any clue if this is the right way?
My model:
class TeachingClasses(models.Model):
name = models.Charfield('Class Name',max_lenght=64)
[...]
status = models.BooleanField('Status',default=True)
What you did is not the correct syntax, you need:
class TeachingClassesAdmin(admin.ModelAdmin):
exclude = ('status',)
admin.site.register(TeachingClasses, TeachingClassesAdmin)
Django doc about how to use exclude.
In the admin.py:
class TeachingClassesAdmin(admin.ModelAdmin):
list_display = ('name',) # plus any other fields you'd like to display
admin.site.register(TeachingClasses, TeachingClassesAdmin)`
I was wondering how they made it possible to display more fields in the User page of the Django admin site.
If you create a new User you only have some basic fields to fill in, but if you reopen that user (edit mode) then you see a lot more fields to fill in.
I'm trying to achieve the same, I had a look at the add_form.html template but I can't really get my head around it. I guess I'm looking for a way of specifying different fields = [] sets based on the edit status of the document.
Thanks!
The answer lies in the custom admin class registered for the User model. It overrides a couple of methods on ModelAdmin and checks to see whether the current request is creating a new User (in which case the bare-bones form class for adding accounts is used) or editing an existing one (in which case a full form is shown).
Here's my try. When I try to create a new item (Add) it shows only certain fields but then when I hit save it returns an error:
DoesNotExist
in /Library/Python/2.6/site-packages/django/db/models/fields/related.py in get, line 288
admin.py
from django.contrib import admin
from myapp.catalog.models import Model
from myapp.catalog.forms import ProductAdminForm, ProductAddForm
class ProductAdmin(admin.ModelAdmin):
form = ProductAdminForm
#...
add_form = ProductAddForm
def get_form(self, request, obj=None, **kwargs):
defaults = {}
if obj is None:
defaults.update({
'form': self.add_form,
})
defaults.update(kwargs)
return super(ProductAdmin, self).get_form(request, obj, **defaults)
forms.py
from myapp.catalog.models import Product
class ProductAdminForm(forms.ModelForm):
class Meta:
model = Product
#...
class ProductAddForm(forms.ModelForm):
class Meta:
model = Product
fields = ("model", "colour",)