Django admin class model issue - django

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?

Related

Django 1.11 admin form add search box to oneToOne field

I have a user, admin and employee models, both employee and admin are related to the user model with a oneToOne field what i would like to do is in the admin form i have one field "user"
instead of having a drop down field with all users i would like to have a search box so that when i search for a specific user i find him and choose him to be an admin.
how?
i also tried to add user's fields to admin's form but i couldn't
i tried the inline thing, the parent_link in the admin's model... but i couldn't find a solution
Now i would like to minimise the search through the long users list by adding a search box to the field
I have solved this type of problem using django-ajax-selects package. Hope this will help you.
app/admin.py:
from ajax_select import register, LookupChannel
#register('users')
class UsersLookup(LookupChannel):
model = User
def get_query(self, q, request):
return self.model.objects.filter(username__icontains=q)
class EmployeeAdminForm(forms.ModelForm):
user = AutoCompleteSelectField('users', required=False,
help_text=None)
class Meta:
model = Employee
fields = '__all__'
settings.py
AJAX_LOOKUP_CHANNELS = {
'users' : {'model': 'auth.user', 'search_field': 'username'},
}
Use django's raw_id_fields. With that you can declare a foreign key object searchable.

django-fluent-comments model doesn't appear in Wagtail ModelAdmin

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.

Django use of readonly_fields

I've been experimenting with the Django admin,
Can someone explain what use readonly_fields have and what difference it makes of just having the field as part of list_display and not on list_editable
The difference is that readonly_fields applies to the edit view for a single object rather than the list view for all objects.
In the list view, list_editable controls what can be edited directly from the list view, and list_display controls what model fields are displayed in the list.
In the individual edit view, readonly_fields controls which fields are displayed but not editable. All other fields will be editable through the ModelForm:
By default the admin shows all fields as editable. Any fields in this option will display its data as-is and non-editable; they are also excluded from the ModelForm used for creating and editing.

Hiding a model field in Django admin 1.9

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)`

How can I convert a ModelChoiceField into a display only field in the admin

Right now, I have an admin page with a Container and a stackedInline for the Pages. The pages have 2 ForeignKeys in them and as the # of values for those ForeignKeys grows, the page takes forever to render.
How do I manage to display the value of the ForeignKey, but NOT let it be a changeable thing in the admin? I definetely want to display the str(ForeignModel) in the inlines, but I don't want a select box with several 1000 choices to be loaded once for each Page model.
I remember reading about this somewhere, but can't seem to locate it anymore. If it wasn't in the admin, I could just change out the widget, but I don't know how to do that in the admin.
Are you talking about ModelAdmin.readonly_fields?
class MyModelAdmin(admin.ModelAdmin):
# Other stuff
readonly_fields = ('myforeignkey',)
And, for future reference, it's easy to change out the widget in the admin.
For a specific field:
class MyModelAdminForm(forms.ModelForm):
class Meta:
model = MyModel
widgets = {
'somefield': MyWidget(),
}
class MyModelAdmin(admin.ModelAdmin):
form = MyModelAdminForm
For all fields of a particular type:
class MyModelAdmin(admin.ModelAdmin):
formfield_overrides = {
models.TextField: {'widget': RichTextEditorWidget},
}