1
我想将标准django textarea widget
注册为管理员。现在我对此有点困惑,因为在文档中我有ModelAdmin.formfield_overrides
的例子,我不想重写它,我只想注册它。 有没有一些简单的方法来做这个注册,或者我应该重写它,并使其自定义的部件吗?在管理员注册django textarea小部件
models.py:
from django.db import models
from django_countries.fields import CountryField
import datetime
# Create your models here.
class ContactForm(models.Model):
first_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
email = models.EmailField(max_length=250)
mobile = models.CharField(max_length=32, blank=True)
timestamp = models.DateTimeField(default=datetime.datetime.now)
birthday = models.DateField(null=True)
move_in_date = models.DateField(null=True)
move_out_date = models.DateField(null=True)
country = CountryField()
def __unicode__(self):
"""TODO: Docstring for __unicode__.
:returns: TODO
"""
return self.email
class Meta:
ordering = ['-timestamp']
forms.py:
from django.forms import ModelForm, extras
from .models import ContactForm
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout
from crispy_forms.bootstrap import TabHolder, Tab
from django_countries.widgets import CountrySelectWidget
import datetime
def get_move_date_field():
"""TODO: Docstring for get_move_date_field.
:returns: return a DateField suitable for move_in_date and move_out_date
"""
return forms.DateField(widget=extras.SelectDateWidget(), initial=datetime.date.today())
class ContactView(ModelForm):
"""TODO: Docstring for ContactView.
:returns: ContactView is a class in which we are returning about user information.
"""
birthday = forms.DateField(widget=extras.SelectDateWidget(years=range(2025, 1939, -1)))
move_in_date = get_move_date_field()
move_out_date = get_move_date_field()
message = forms.CharField(widget=forms.Textarea)
helper = FormHelper()
helper.form_tag = False
helper.layout = Layout(
TabHolder(
Tab(
'Basic Information',
'first_name',
'last_name',
'email',
'birthday',
'country',
'mobile'
),
Tab(
'Moving and additional Information',
'move_in_date',
'move_out_date',
'message',
)
)
)
class Meta:
fields = ['first_name', 'last_name', 'email', 'mobile',
'birthday', 'move_in_date', 'move_out_date',
'country', 'message']
model = ContactForm
widgets = {
'country': CountrySelectWidget()
}
admin.py:
from django.contrib import admin
from .models import ContactForm
# Register your models here.
class ContactFormAdmin(admin.ModelAdmin):
"""Docstring for ContactFormAdmin. """
class Meta:
model = ContactForm
admin.site.register(ContactForm, ContactFormAdmin)
问题解决了? –
是的,这是一个愚蠢的错误。 – PetarP