简体   繁体   English

在管理员中注册django textarea小部件

[英]register django textarea widget in admin

I want to register standard django textarea widget to admin. 我想将标准django textarea widget注册为admin。 Now I'm a little confused about this, because in documentation I have ModelAdmin.formfield_overrides example, I don't want to override it, I just want to register it. 现在我对此有些困惑,因为在文档中有ModelAdmin.formfield_overrides示例,我不想覆盖它,我只想注册它。 Is there some simple way of doing this registration or should I override it and make a custom widget out of it? 是否有一些简单的方法可以进行此注册,还是应该覆盖它并从中制作自定义小部件?

models.py: 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: 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: 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)

I just forgot to register message inside `models.py'. 我只是忘了在`models.py'中注册消息。

message = models.CharField(max_length=1000) 消息= models.CharField(max_length = 1000)

after makemigration and migrate I had textarea inside django admin . 之后makemigrationmigratetextarea里面django admin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM