2013-05-07 86 views
2

我試圖在管理中添加一個「事件」,並得到這個錯誤:類型錯誤,「Class對象」對象不標化

TypeError at /admin/sms/event/add/ 'Contact' object is not subscriptable

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/' 
+0

它將它添加到數據庫,然後拋出錯誤? – karthikr 2013-05-07 16:15:06

+0

你有定製的管理模式嗎?如果是這樣,請發佈我們的admin.py – Aldarund 2013-05-07 16:36:23

回答

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 
相關問題