Django: Admin: changing the widget of the field in Admin

UPDATE 1: Code that gets me done with 1) (don’t forget tot pass CHOICES to the BooleanField in the model) from main.models import TagCat from django.contrib import admin from django import forms class MyTagCatAdminForm(forms.ModelForm): class Meta: model = TagCat widgets = { ‘by_admin’: forms.RadioSelect } fields=”__all__” # required for Django 3.x class TagCatAdmin(admin.ModelAdmin): form = … Read more

Filter ManyToMany box in Django Admin

Ok, this is my solution using above classes. I added a bunch more filters to filter it correctly, but I wanted to make the code readable here. This is exactly what I was looking for, and I found my solution here: http://www.slideshare.net/lincolnloop/customizing-the-django-admin#stats-bottom (slide 50) Add the following to my admin.py: class CustomerForm(forms.ModelForm): def __init__(self, *args, … Read more

How do you change the default widget for all Django date fields in a ModelForm?

You can declare an attribute on your ModelForm class, called formfield_callback. This should be a function which takes a Django model Field instance as an argument, and returns a form Field instance to represent it in the form. Then all you have to do is look to see if the model field passed in is … Read more

How do I use Django’s MultiWidget?

Interesting question and I think perhaps deserving of a little more attention in the docs. Here’s an example from a question I’ve just asked: class DateSelectorWidget(widgets.MultiWidget): def __init__(self, attrs=None, dt=None, mode=0): if dt is not None: self.datepos = dt else: self.datepos = date.today() # bits of python to create days, months, years # example below, … Read more

Django Admin: Using a custom widget for only one model field

Create a custom ModelForm for your ModelAdmin and add ‘widgets’ to its Meta class, like so: class StopAdminForm(forms.ModelForm): class Meta: model = Stop widgets = { ‘field_name’: ApproveStopWidget(), } fields=”__all__” class StopAdmin(admin.ModelAdmin): form = StopAdminForm Done! Documentation for this is sort of non-intuitively placed in the ModelForm docs, without any mention to it given in … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)