2

我正在尝试在管理员中添加一个“事件”并收到此错误:

/admin/sms/event/add/ 'Contact' 对象的 TypeError 不可下标

models.py:

 class Contact(models.Model):
    users = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name="contact")
    name = models.CharField(_("Recipient"), max_length=40)
    phone = models.IntegerField(_("Phone"), max_length=10)

    def __unicode__(self):
        return "%s: %d" % (self.name, self.phone)

class Event(models.Model):
    calendar = models.ForeignKey(Calendar, verbose_name=_("Calendar"), related_name="event_calendar")
    message = models.ForeignKey(Message, verbose_name=_("Message"), related_name="event_message")
    recipient = models.ForeignKey(Contact, verbose_name=_("Recipient"), related_name="event1")
    event_date = models.DateField(_("Date"))
    start_time = models.TimeField(_("Start time"))
    end_time = models.TimeField(_("End time"), blank=True, null=True)
    location = models.CharField(_("Location of meeting"), blank=True, null=True, max_length=100)
    reminder_options = models.IntegerField(choices=ReminderOptions.CHOICES, verbose_name=_("Reminder time"))
    content = models.CharField(_("Event Notes"), max_length=160)
    # recurring_options = models.IntegerField(choices=RecurringOptions.CHOICES, verbose_name=_("Recurring time"))

    def __unicode__(self):
        return self.recipient

    def get_absolute_url(self):
        return u'/create-event/'
4

1 回答 1

0

首先猜测是您的电话字段从您的管理表单中获取了一个字符串。在模型中使用 charField 作为电话,并在 admin.py 中创建自定义表单。

在 models.py 中将您的手机 Integerfield 更改为 CharField:

class Contact(models.Model):
    ...
    phone = models.CharField(_("Phone"), max_length=200)

    def __unicode__(self):
        return "%s: %s" % (self.name, self.phone) # %d becomes %s

在 Admin.py 创建一个表单:

from models import Contact
from django import forms
from django_localflavor_fr.forms import FRPhoneNumberField

class ContactForm(forms.ModelForm):
    phone = FRPhoneNumberField()
    class Meta:
        model = Contact

https://docs.djangoproject.com/en/1.5/ref/contrib/localflavor/

在 Admin.py 中创建一个 ModelAdmin:

class ContactAdmin(admin.ModelAdmin):
    form = ContactForm
于 2013-05-08T14:43:12.563 回答