django-widget

Grouping CheckboxSelectMultiple Options in Django

删除回忆录丶 提交于 2019-11-28 01:26:40
问题 In my Django App I have the following model: class SuperCategory(models.Model): name = models.CharField(max_length=100,) slug = models.SlugField(unique=True,) class Category(models.Model): name = models.CharField(max_length=100,) slug = models.SlugField(unique=True,) super_category = models.ForeignKey(SuperCategory) What I'm trying to accomplish in Django's Admin Interface is the rendering of Category using widget CheckboxSelectMultiple but with Category somehow grouped by SuperCategory ,

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

主宰稳场 提交于 2019-11-27 17:41:06
I have a DateTimeField field in my model. I wanted to display it as a checkbox widget in the Django admin site. To do this, I created a custom form widget. However, I do not know how to use my custom widget for only this one field. The Django documentation explains how to use a custom widget for all fields of a certain type: class StopAdmin(admin.ModelAdmin): formfield_overrides = { models.DateTimeField: {'widget': ApproveStopWidget } } This is not granular enough though. I want to change it for only one field. Chris Pratt Create a custom ModelForm for your ModelAdmin and add 'widgets' to its

Django subclassing multiwidget - reconstructing date on post using custom multiwidget

只愿长相守 提交于 2019-11-27 17:33:00
问题 So my django book is back at university and I'm struggling to work this one out. I've subclassed django.forms.widgets.MultiWidget like so: 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, the rest snipped for neatness. years = [(year, year) for year in year_digits] _widgets = ( widgets.Select(attrs=attrs, choices

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

感情迁移 提交于 2019-11-27 06:06:51
Given a set of typical models: # Application A from django.db import models class TypicalModelA(models.Model): the_date = models.DateField() # Application B from django.db import models class TypicalModelB(models.Model): another_date = models.DateField() ... How might one change the default widget for all DateFields to a custom MyDateWidget? I'm asking because I want my application to have a jQueryUI datepicker for inputting dates. I've considered a custom field that extends django.db.models.DateField with my custom widget. Is this the best way to implement this sort of across-the-board change

How do I use Django's MultiWidget?

不羁的心 提交于 2019-11-26 23:51:56
问题 The documentation is a bit lacking with respect to this feature. from django import forms class TwoInputWidget(forms.MultiWidget): """An example widget which concatenates two text inputs with a space""" def __init__(self, attrs=None): widgets = [forms.TextInput, forms.TextInput] I can see I need to create a "widgets" property with a list of other widgets, but after that it gets a little Sherlock Holmes. Would someone please explain to me how to use the MultiWidget widget? 回答1: Interesting

Filter ManyToMany box in Django Admin

半腔热情 提交于 2019-11-26 23:49:19
问题 I have a object with a many-to-many relation with another object. In the Django Admin this results in a very long list in a multiple select box. I'd like to filter the ManyToMany relation so I only fetch Categories that is available in the City that the Customer have selected. Is this possible? Will I have to create a widget for it? And if so - how do I copy the behavior from the standard ManyToMany field to it, since I would like the filter_horizontal function as well. These are my

Django: How do I add arbitrary html attributes to input fields on a form?

若如初见. 提交于 2019-11-26 21:38:08
I have an input field that is rendered with a template like so: <div class="field"> {{ form.city }} </div> Which is rendered as: <div class="field"> <input id="id_city" type="text" name="city" maxlength="100" /> </div> Now suppose I want to add an autocomplete="off" attribute to the input element that is rendered, how would I do that? Or onclick="xyz()" or class="my-special-css-class" ? Check this page city = forms.CharField(widget=forms.TextInput(attrs={'autocomplete':'off'})) Sorry for advertisment, but I've recently released an app ( https://github.com/kmike/django-widget-tweaks ) that

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

穿精又带淫゛_ 提交于 2019-11-26 19:05:17
问题 I have a DateTimeField field in my model. I wanted to display it as a checkbox widget in the Django admin site. To do this, I created a custom form widget. However, I do not know how to use my custom widget for only this one field. The Django documentation explains how to use a custom widget for all fields of a certain type: class StopAdmin(admin.ModelAdmin): formfield_overrides = { models.DateTimeField: {'widget': ApproveStopWidget } } This is not granular enough though. I want to change it

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

柔情痞子 提交于 2019-11-26 11:51:57
问题 Given a set of typical models: # Application A from django.db import models class TypicalModelA(models.Model): the_date = models.DateField() # Application B from django.db import models class TypicalModelB(models.Model): another_date = models.DateField() ... How might one change the default widget for all DateFields to a custom MyDateWidget? I\'m asking because I want my application to have a jQueryUI datepicker for inputting dates. I\'ve considered a custom field that extends django.db

Django: How do I add arbitrary html attributes to input fields on a form?

倖福魔咒の 提交于 2019-11-26 08:55:22
问题 I have an input field that is rendered with a template like so: <div class=\"field\"> {{ form.city }} </div> Which is rendered as: <div class=\"field\"> <input id=\"id_city\" type=\"text\" name=\"city\" maxlength=\"100\" /> </div> Now suppose I want to add an autocomplete=\"off\" attribute to the input element that is rendered, how would I do that? Or onclick=\"xyz()\" or class=\"my-special-css-class\" ? 回答1: Check this page city = forms.CharField(widget=forms.TextInput(attrs={'autocomplete':