web-dev-qa-db-ja.com

TypeError 'x'オブジェクトには属性 '__getitem__'がありません

エラーが発生しました

'CampSite' object has no attribute '__getitem__'

管理インターフェイスで新しいCampConエントリを保存すると、キャンプサイトのデータベースを処理するキャンプサイトと呼ばれるアプリケーションが1つあり、レビューを処理するコンテンツと呼ばれる別のアプリケーションがあります。私はDjango=に非常に慣れていません。これは私の最初の非チュートリアルベースのプロジェクトです。私は頭を悩ませてWebで検索して運が悪かったです。助けてくれてありがとう。これが私のキャンプサイトモデルです。

from Django.contrib.gis.db import models

class CampSite(models.Model):
    name = models.CharField(max_length=250)
    showers = models.BooleanField()
    flush = models.BooleanField()
    rv = models.BooleanField()
    lonlat = models.PointField(null=True, blank=True)

    objects = models.GeoManager()

    def __unicode__(self):
        return self.name

これがコンテンツモデルです。

from Django.contrib.gis.db import models
from campsites.models import CampSite
from Django.contrib.auth.models import User
from activities.models import Activities


class CampCon(models.Model):
    campsite = models.ForeignKey(CampSite)
    trip = models.ForeignKey('Trip')
    Date = models.DateField()
    user = models.ForeignKey(User)
    overall_review = models.TextField()
    facilities_review = models.IntegerField()
    things_to_do = models.IntegerField()
    privacy = models.IntegerField()
    beauty = models.IntegerField()
    overall_rating = models.IntegerField()

    def __unicode__(self):
        return self.campsite

class ImageDB(models.Model):
    campsite = models.ForeignKey(CampSite)
    user = models.ForeignKey(User)
    description = models.CharField(max_length=200)
    image = models.ImageField(upload_to='/home/bobby/Pictures/CampThat')
    date_uploaded = models.DateField()
    date_taken = models.DateField()
    trip = models.ForeignKey('Trip')
    activity = models.ForeignKey(Activities)

    def __unicode__(self):
        return self.campsite

class Trip(models.Model):
    date = models.DateField()
    description = models.CharField(max_length=100)
    title = models.CharField(max_length=100)
    blog = models.TextField()

    def __unicode__(self):
        return self.title

class ActivityCon(models.Model):
    trip = models.ForeignKey(Trip)
    rating = models.IntegerField()
    overall_review = models.TextField()
    activity = models.ForeignKey(Activities)

そしてここに完全なトレースバックがあります:

    Environment:


Request Method: POST
Request URL: http://localhost:8000/admin/content/campcon/add/

Django Version: 1.5
Python Version: 2.7.3
Installed Applications:
('Django.contrib.auth',
 'Django.contrib.contenttypes',
 'Django.contrib.sessions',
 'Django.contrib.sites',
 'Django.contrib.messages',
 'Django.contrib.staticfiles',
 'campsites',
 'auth',
 'trips',
 'content',
 'activities',
 'Django.contrib.admin')
Installed Middleware:
('Django.middleware.common.CommonMiddleware',
 'Django.contrib.sessions.middleware.SessionMiddleware',
 'Django.middleware.csrf.CsrfViewMiddleware',
 'Django.contrib.auth.middleware.AuthenticationMiddleware',
 'Django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/usr/lib/python2.7/site-packages/Django/core/handlers/base.py" in get_response
  116.                         response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.7/site-packages/Django/contrib/admin/options.py" in wrapper
  367.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/Django/utils/decorators.py" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/Django/views/decorators/cache.py" in _wrapped_view_func
  89.         response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/Django/contrib/admin/sites.py" in inner
  202.             return view(request, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/Django/utils/decorators.py" in _wrapper
  25.             return bound_func(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/Django/utils/decorators.py" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/Django/utils/decorators.py" in bound_func
  21.                 return func(self, *args2, **kwargs2)
File "/usr/lib/python2.7/site-packages/Django/db/transaction.py" in inner
  208.                 return func(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/Django/contrib/admin/options.py" in add_view
  958.                 self.log_addition(request, new_object)
File "/usr/lib/python2.7/site-packages/Django/contrib/admin/options.py" in log_addition
  525.             action_flag     = ADDITION
File "/usr/lib/python2.7/site-packages/Django/contrib/admin/models.py" in log_action
  18.         e = self.model(None, None, user_id, content_type_id, smart_text(object_id), object_repr[:200], action_flag, change_message)

Exception Type: TypeError at /admin/content/campcon/add/
Exception Value: 'CampSite' object has no attribute '__getitem__'
13
bcoop713

あなたの問題は、Unicode文字列を返す必要があるときに___unicode___関数がモデルオブジェクトを返すことです。

これを行うには、unicode()関数を___unicode___メソッドに追加します。

_class CampCon(models.Model):
    campsite = models.ForeignKey(CampSite)
    trip = models.ForeignKey('Trip')
    Date = models.DateField()
    user = models.ForeignKey(User)
    overall_review = models.TextField()
    facilities_review = models.IntegerField()
    things_to_do = models.IntegerField()
    privacy = models.IntegerField()
    beauty = models.IntegerField()
    overall_rating = models.IntegerField()

    def __unicode__(self):
        return unicode(self.campsite)

class ImageDB(models.Model):
    campsite = models.ForeignKey(CampSite)
    user = models.ForeignKey(User)
    description = models.CharField(max_length=200)
    image = models.ImageField(upload_to='/home/bobby/Pictures/CampThat')
    date_uploaded = models.DateField()
    date_taken = models.DateField()
    trip = models.ForeignKey('Trip')
    activity = models.ForeignKey(Activities)

    def __unicode__(self):
        return unicode(self.campsite)
_

これは_CampSite.__unicode___を呼び出し、_campsite.name_を返します。

30

代わりにこのメソッドを使用してください:

   def __unicode__(self):
        return unicode(self.campsite)
2
santiagobasulto

これはGoogleでの最初のヒットなので、次のようにすると、同様のエラー(_'ItemGroup' object has no attribute '__getitem__'_)が発生しました。

_class ItemGroup(models.Model):
    groupname = models.CharField(max_length=128)

    def __unicode__(self):
        return "%s" % self.groupname

class Item(models.Model):
    name = models.CharField(max_length=128)
    group = models.ForeignKey(MedGroup, verbose_name="Groep")

    def __unicode__(self):
        return "%s (%s)" % (self.name, self.group[:10])
_

最後の行が間違っています。

その行をreturn "%s (%s)" % (self.name, self.group.groupname[:10])に置き換えることで修正されました

0
SaeX

これは、次のようにした場合にも発生します。

event_name = CharField(max_length = 250)

このようではありません:(正しい方法)

event_name = models.CharField(max_length = 250)

誰かに役立つかもしれません

0
Ramez Ashraf