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

前端 未结 6 1006
夕颜
夕颜 2020-11-30 18:48

Given a set of typical models:

# Application A
from django.db import models
class TypicalModelA(models.Model):
    the_date = models.DateField()

 # Applicat         


        
6条回答
  •  有刺的猬
    2020-11-30 19:22

    You do want to define a custom widget, and use the widget's inner Media class to define the JS (and CSS?) files that have to be included in the page for the widget to work. If you do this right, you can make your widget completely self-contained and reusable. See django-markitup for one example of doing this (it has a reusable widget for the MarkItUp! universal markup editor).

    Then use formfield_callback (see James Bennett's answer) to easily apply that widget to all DateField's in a form.

提交回复
热议问题