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)
Related
In the default Django Admin Site, if a model is registered with ForegingKey fields and those are included in readonly_fields (using the model property or the get_readonly_fields method) the value of the field is rendered with a link to the Change View, but this Doesn't work on a custom Django Admin Site.
E.g.: I have this two models:
class ModelA(models.Model):
field_a = models.CharField(max_length=50)
class ModelB(models.Model):
model_a = models.ForeignKey(ModelA, on_delete=models.PROTECT)
I registered in the default Django Admin Site:
admin.register(ModelA)
#register(ModelB)
class ModelBAdmin(admin.ModelAdmin):
model = ModelB
readonly_fields = ["model_a"]
So I get in the Change View of ModelB in the Admin the value of the field (str of the model) with a link to the Change View of the related model:
Link pointing to Chang View
But if I register the models in a Custom Admin Site, the link is not generated.
How can I extends my Custom Admin Site in order to generate those links?
PD1: I know I can code custom methods to build the links, but this is not a DRY way of do it and doesn't work well with get_readonly_fields method
PD2: If I register the related model (ModelA in the example) in the default Admin Site the link is genereted, but point to the default Admin Site, brokening the purpose of the Custom Admin Site.
I posted the same question in the official Django forum:
forum.djangoproject.com/t/9472/5
It has generated a ticket because is a missing line in the method get_admin_url in the helper class AdminReadOnlyField, The temporal solution is in the ticket: code.djangoproject.com/ticket/33077
PD: Why someone marks this post like a bad question? It has generates a ticket for a patch so it was a fair problem, this is my first question in StackOverflow and this is discouraging.
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
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 have three models - two of them are in one app, and the third one is on the another. The structure is like this:
taapp.models:
class Teachers(model.Model):
fullname = models.CharField(max_length=50)
...
class TeachersScale(model.Model):
teacher = models.ForeignKey("Teachers")
abbr = models.ForeignKey("questions.QuestionTypes")
questions.models:
class QuestionTypes(models.Model):
abbr = models.CharField(max_length=5)
......
I registered all these models to admin:
taapp.admin:
from taapp.models import Teachers
from taapp.models import TeachersScale
from django.contrib import admin
from admin_forms import TeachersAdmin, TeachersScaleAdmin
admin.site.register(Teachers, TeachersAdmin)
admin.site.register(TeachersScale, TeachersScaleAdmin)
taapp.admin_forms:
from django import forms
from django.contrib import admin
class TeachersAdmin(admin.ModelAdmin):
list_display = ('fullname', 'email', 'registration_date')
class TeachersScaleAdmin(admin.ModelAdmin):
list_display = ('teacher', 'abbr')
list_filter = ['teacher','abbr']
When I try to add a field to TeachersScale in admin site, I get the following error:
DatabaseError at /admin/taapp/teachersscale/add/
(1146, "Table 'taapp.questions_questiontypes' doesn't exist")
It treats QuestionTypes, as it is a model in taapp. How to solve it? Or is there something wrong with my db design?
I tried TabularInline for QuestionTypes to see if reverse adding works. Well, it works:
questions.admin:
class TeachersScaleInline(admin.TabularInline):
model = TeachersScale
class QuestionTypesAdmin(admin.ModelAdmin):
inlines = [TeachersScaleInline]
Thanks in advance.
It looks like you haven't actually created your questions table, or if you have you've forced it into a different database. Foreign keys expect to share the same database, and it's perfectly standard to have multiple apps sharing the same database. That's why the app name is part of the automatically generated table name.
In Django admin I want to override and implement my own form for a model (e.g. Invoice model).
I want the invoice form to have auto-fill fields for customer name, product name and I also want to do custom validation (such as credit limit for a customer). How can I override the default form provided by Django admin and implement my own?
I am new to Django, I appreciate any pointers.
You can override forms for django's built-in admin by setting form attribute of ModelAdmin to your own form class. See:
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-custom-validation-to-the-admin
It's also possible to override form template - have a look at https://docs.djangoproject.com/en/dev/ref/contrib/admin/#custom-template-options
If you're looking specifically for autocomplete I can recommend https://github.com/crucialfelix/django-ajax-selects
How to override a form in the django admin according to the docs:
from django import forms
from django.contrib import admin
from myapp.models import Person
class PersonForm(forms.ModelForm):
class Meta:
model = Person
exclude = ['name']
class PersonAdmin(admin.ModelAdmin):
exclude = ['age']
form = PersonForm