在admin中注册django textarea小部件

时间:2016-03-01 05:45:10

标签: python django django-models django-forms django-admin

我想将标准django textarea widget注册到管理员。现在我对此有点困惑,因为在文档中我有ModelAdmin.formfield_overrides示例,我不想覆盖它,我只想注册它。 是否有一些简单的方法来进行此注册,还是应该覆盖它并从中创建自定义小部件?

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)

1 个答案:

答案 0 :(得分:0)

我忘记在`models.py'中注册消息。

message = models.CharField(max_length = 1000)

makemigrationmigrate之后我textareadjango admin