I want to edit boolean values from the list page in Wagtail Admin. I cant seam to get this functionality working in Wagtail as its built upon Django i believe it can work on Wagtail as well but i cant figure it out.
Django way of doing it:
class TaskAdmin(models.ModelAdmin):
list_display = (..., 'boolean_field')
list_editable = ('boolean_field',)
Thank you
There is a way to add site settings to the Wagtail admin. Is this what you're looking for? You can make boolean toggle switches in the settings menu and then pass the values to a view or a template. It would look something like this:
from django.db import models
from wagtail.contrib.settings.models import BaseSetting, register_setting
#register_setting
class TaskAdmin(BaseSetting):
list_display = models.BooleanField()
list_editable = models.BooleanField()
Docs are here: https://docs.wagtail.io/en/stable/reference/contrib/settings.html
Related
I'm building a django app and the django admin page doesn't seem to be reflecting the changes I make to admin.py. For example if I want to exclude some fields, or customizing the admin change list, nothing changes in the actual page. The only thing that seems to be reflected properly is the fact that I can register the models, and they will show up.
Here's my admin.py
from django.contrib import admin
from .models import Form, Biuletyn, Ogloszenie, Album
class FormAdmin(admin.ModelAdmin):
exclude = ('img',)
class BiuletynAdmin(admin.ModelAdmin):
list_display = ('name', 'date')
class OgloszenieAdmin(admin.ModelAdmin):
fields = ('name', 'date', 'upload')
admin.site.register(Form)
admin.site.register(Biuletyn)
admin.site.register(Ogloszenie)
admin.site.register(Album)
P.S. Please ignore the weird model names. The site is actually in a different language :D
This is how you register your ModelAdmin:
admin.site.register(Form, FormAdmin)
admin.site.register(Biuletyn, BiuletynAdmin)
admin.site.register(Ogloszenie, OgloszenieAdmin)
This how to register your Models:
admin.site.register(Album)
For more details you may refer to Django official documentation at: https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#modeladmin-objects
Add admin_class
So for example for the Form model:
admin.site.register(Form, admin_class=FormAdmin)
I need use inlines in django admin for show relation between two models but in the moment that i do, i had to do the reverse relationship to show inlines.
Example:
class OtherModel(models.Model):
field1=models...
........
class Model(models.Model)
field1 = models....
other_model = models.ForeignKey(OtherModel)
I create the inline...
class OtherModelInline(admin.StackedInline):
model = OtherModel
extra = 1
#admin.register(Model):
class ModelAdmin(admin.modelAdmin):
inlines = [OtherModelInline]
So...
When I create the Inline it required foreign key on OtherModel..
How can I show this without change the relationship?
This is the right way to do it.
If you want to use inlines, you need to specify that these two models are somewhat related.
The Django admin panel has a search autocomplete with search_fields on the list of objects of a model, but right now I have 3000 users. To add a user manually is dificult with the selectbox; I need the same behavior like the searchfields for selecting a user foreinkey User.
How can I include the Django search feature on the form for editing inside the admin panel?
from myapp.models import Red
from django.contrib.auth.models import User
class Red(models.Model):
customer = models.ForeignKey(User, verbose_name="Cliente")
pub_date = models.DateTimeField(default=datetime.now, blank=True)
Since Django 2.0, you can use autocomplete_fields to generate autocomplete fields for foreign keys.
class UserAdmin(admin.ModelAdmin):
search_fields = ['username', 'email']
class RedAdmin(admin.ModelAdmin):
autocomplete_fields = ['customer']
Django has no built-in autocomplete functionality for foreign keys on admin but the raw_id_fields option may help:
class RedAdmin(admin.ModelAdmin):
raw_id_fields = ("customer", )
If you want real autocomplete then you have to use 3rd-party app like django-autocomplete-light or some of the other solutions.
Given the following example models:
class Reporter(models.Model):
pass
class Article(models.Model):
reporter = models.ForeignKey(Reporter)
I want to define a ReporterForm that would allow you to add/edit articles for that reporter and also to edit the reporter's own fields. (non-existent in the example code)
I want to be able to use ReporterForm in the django admin panel so that whenever a reporter is edited, the admin can also see the articles that belong to that reporter inline.
Is there a clean way to do this? Or is this not the right model design to start with?
PS. I considered giving Reporter a ManyToManyField(Article) and just letting django do its magic but that implies that Articles can belong to many different Reporters, doesn't it?
This is exactly what inline formsets are for. You can have inline model formsets:
https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#inline-formsets
To do this in the admin site there is InlineModelAdmin:
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#inlinemodeladmin-objects
from django.contrib import admin
class ArticleInline(admin.TabularInline):
model = Article
class ReporterAdmin(admin.ModelAdmin):
inlines = [
ArticleInline,
]
I'm trying to get an attribute of a model to show up in the Django admin change/add page of another model. Here are my models:
class Download(model.Model):
task = models.ForeignKey('Task')
class Task(model.Model):
added_at = models.DateTimeField(...)
Can't switch the foreignkey around, so I can't use Inlines, and of course fields = ('task__added_at',) doesn't work here either.
What's the standard approach to something like this? (or am I stretching the Admin too far?)
I'm already using a custom template, so if that's the answer that can be done. However, I'd prefer to do this at the admin level.
If you don't need to edit it, you can display it as a readonly field:
class DownloadAdmin(admin.ModelAdmin):
readonly_fields = ('task_added_at',)
def task_added_at(self, obj):
return obj.task.added_at