Change field in Django Flatpages Admin - django

Using Flatpages with the default admin, I need to change the template field from a text input with to select or radio with predefined choices. It's easy to do this with one of my own apps - just use the choices attribute in the model.
I have tried a few things - I will add details about those attempts later if necessary - but does anyone know a nice way to do this?

Define a custom flatpages ModelAdmin class which inherits from the default one but uses a custom form. On this form, override the field, using the widget you want. Then unregister the flatpages admin and reregister it with your custom class.
from django.contrib.flatpages.admin import FlatPageAdmin, FlatpageForm
class MyFlatpageForm(FlatpageForm):
template = forms.ChoiceField(choices=MY_CHOICES)
class MyFlatPageAdmin(FlatPageAdmin):
form = MyFlatpageForm
admin.site.unregister(FlatPage)
admin.site.register(FlatPage, MyFlatPageAdmin)

Related

Is it possible to allow to create objects from the list_display view of the django admin site?

I have a model with fields name, roll_no, birth_date
I am using the django admin's list display and list editable to have these fields displayed and edited in a list format in a single page. However, to add a new entry I have to go to the create_form page.
Is it possible to simply add new objects from the list_display page itself?
Unfortunately this feature is not available out-of-the box in the Django admin like the ModelAdmin.list_editable feature.
I'm curious to see if there are other shortcuts, but at the moment the only way I see is to customize the formset like descibed in the official Docs:
from django import forms
class MyForm(forms.ModelForm):
# customize your 'extra' forms here
class MyModelAdmin(admin.ModelAdmin):
def get_changelist_form(self, request, **kwargs):
return MyForm
And finally manually extend the changelist form template of the admin. To override a Django admin template, please follow the intructions in the Official Docs here. The template to be customized is the following folder:
.../django/contrib/admin/templates/admin/change_list.html
and you probably need to override the {% block result_list %} in that file.
NB: the customization of an admin template can be very tricky. Consider to use a CMS (like DjangoCMS) if you need to extend the user experience. The idea behind the Django admin is to make your life easier with an out-of-the-box interface for CRUDs on your DB. IMHO try to avoid complex customizations of the Django Admin if not strictly needed.

Django admin: created users have plain text password and can't login

I want to have a custom tab for staff users so in admin.py I do:
class StaffUser(User):
class Meta:
proxy = True
#admin.register(StaffUser)
class AdminUserAdmin(admin.ModelAdmin):
pass
But on the admin site, whenever I add a new user with this interface I can't log in with it since for some reason it sets it's password as plain text instead of hashing it.
I've read this post BUT if I do that and change StaffUser to inherint from AdminUserI get this other error
AttributeError: type object 'StaffUser' has no attribute '_meta'
You should declare your models in models.py (or a models package), not admin.py.
UserAdmin (there is no such thing as an AdminUser in the Django packages) is a subclass of ModelAdmin, that is, a class for registering models in the Django admin. It is not a model subclass that you can extend to make new models.
Also, if you are trying to extend the User just to distinguish users with access to the Django admin site, note that there is already an is_staff property in the default User model. And in any case, if you still want to extend the User model, you should be extending AbstractUser instead as stated in the docs.

Create multiple objects in django admin at once

For example, I have a Post model:
Class Post(models.Model):
title = models.Charfield(max_length=200)
# other fields
I wonder is there a way to create multiple posts at once in admin. In other words, I need a formset instead of single form on post creation page.
I've heard recently about a django app that exactly does this job. It's called django-bulk-admin and enables bulk add/update in the admin.
Possibly, the best way to do exactly what you want is extend the ModelAdmin class, because it has no formsets on it, except for those used on InlineFormsets.
After that you could customize the admin change_form template, to include your formsets
The quick-and-dirty way to do it using admin is wrap your Post model as an inline formset of another modeladmin and add the extra option to it.

how to change django model admin url?

I just need to change
http://localhost:8000/admin/song_management/album/
to
http://localhost:8000/admin/song-management/album/
Note:
I am using ModelAdmin & Changed admin template completely ( ex; Used hardcoded url mostly )
Django's ModelAdmin uses the model's app_label per default to generate the urls. The only way to bypass this would be to subclass AdminSite and change the relevant parts.

Defining Default Inline value for Django Admin forms?

If I have an inline admin class like this:
class ResourceInline(admin.TabularInline):
model = Resource
extra = 3
Is there any way to specify the default values for the 3 "extra" resources because the manual says that the prepopulated_fields doesn't accept ForeignKey fields.
You could override formfield_for_foreignkey and set the initial value of your fields.
Check this answer to get the basic idea: Default value for user ForeignKey with Django admin
You can also create default values for your inline extra models by targeting the parent admin class that uses ResourceInline as it's inline.
All you have to do is override the add_view function on the parent admin class:
Customize Django Admin: Add More Than One Default Inline on Parent Add_View