TypeError,'Class object'对象不可订阅

时间:2013-05-07 16:00:27

标签: django django-models django-admin

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

  

/ admin / sms / event / add /中的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/'

1 个答案:

答案 0 :(得分:0)

首先猜测您的手机字段会从您的管理表单中获取字符串。在模型中使用charField for phone,并在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