Django 数组字段“项目”不可下标

Posted

技术标签:

【中文标题】Django 数组字段“项目”不可下标【英文标题】:Djongo ArrayField 'Item' is not subscriptable 【发布时间】:2020-11-22 03:53:36 【问题描述】:

我的 django 项目中有 2 个模型。 DB 是 mongoDB,我用的是 Djongo。

以下是模型:

class Item(models.Model):
    item_id = models.IntegerField(primary_key=True)
    item_name = models.CharField(max_length=30)

    def __str__(self):
        return f'<self.item_name>'


class Items(models.Model):
    items_id = models.IntegerField(primary_key=True)
    inventory = models.ArrayField(model_container=Item,)
    neutral_item = models.EmbeddedField(model_container=Item,)
    buffs = models.ArrayField(model_container=Item,)

    def __str__(self):
        return f'<self.inventory, self.neutral_item>'

当我这样做时:

item = Item(item_name='Helmet')
items = Items(inventory=[item])

我收到这个类型错误:

'Item' object is not subscriptable

怎么了?

整个错误信息:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/georgii/PycharmProjects/custom_hero_chaos/venv/lib/python3.8/site-packages/django/db/models/base.py", line 745, in save
    self.save_base(using=using, force_insert=force_insert,
  File "/Users/georgii/PycharmProjects/custom_hero_chaos/venv/lib/python3.8/site-packages/django/db/models/base.py", line 782, in save_base
    updated = self._save_table(
  File "/Users/georgii/PycharmProjects/custom_hero_chaos/venv/lib/python3.8/site-packages/django/db/models/base.py", line 887, in _save_table
    results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
  File "/Users/georgii/PycharmProjects/custom_hero_chaos/venv/lib/python3.8/site-packages/django/db/models/base.py", line 924, in _do_insert
    return manager._insert(
  File "/Users/georgii/PycharmProjects/custom_hero_chaos/venv/lib/python3.8/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/georgii/PycharmProjects/custom_hero_chaos/venv/lib/python3.8/site-packages/django/db/models/query.py", line 1204, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "/Users/georgii/PycharmProjects/custom_hero_chaos/venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1390, in execute_sql
    for sql, params in self.as_sql():
  File "/Users/georgii/PycharmProjects/custom_hero_chaos/venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1333, in as_sql
    value_rows = [
  File "/Users/georgii/PycharmProjects/custom_hero_chaos/venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1334, in <listcomp>
    [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
  File "/Users/georgii/PycharmProjects/custom_hero_chaos/venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1334, in <listcomp>
    [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
  File "/Users/georgii/PycharmProjects/custom_hero_chaos/venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1275, in prepare_value
    value = field.get_db_prep_save(value, connection=self.connection)
  File "/Users/georgii/PycharmProjects/custom_hero_chaos/venv/lib/python3.8/site-packages/djongo/models/fields.py", line 215, in get_db_prep_save
    return self.get_prep_value(value)
  File "/Users/georgii/PycharmProjects/custom_hero_chaos/venv/lib/python3.8/site-packages/djongo/models/fields.py", line 222, in get_prep_value
    processed_value = self._value_thru_fields('get_prep_value',
  File "/Users/georgii/PycharmProjects/custom_hero_chaos/venv/lib/python3.8/site-packages/djongo/models/fields.py", line 315, in _value_thru_fields
    post_dict = super()._value_thru_fields(func_name,
  File "/Users/georgii/PycharmProjects/custom_hero_chaos/venv/lib/python3.8/site-packages/djongo/models/fields.py", line 145, in _value_thru_fields
    field_value = value[field.attname]
TypeError: 'Item' object is not subscriptable

【问题讨论】:

你能发布整个错误信息吗?想知道它发生在哪里。 请在上面找到,编辑问题 需要明确的是,当您运行 item = Item(item_name...) 时。您是尝试在数据库中创建该实例还是获取它? 我想稍后保存()它 您是否让它与我在下面的回复一起使用? 【参考方案1】:

根据Djongo docs for ArrayField,您应该像这样实例化:

entry = Entry()
entry.authors = ['name': 'John', 'email': 'john@mail.com',
                'name': 'Paul', 'email': 'paul@mail.com']
entry.save()

所以在你的情况下,你会这样做。

items = Items()
items.inventory = [
    'item_name': 'Helmet'
]
items.save()

【讨论】:

以上是关于Django 数组字段“项目”不可下标的主要内容,如果未能解决你的问题,请参考以下文章

Django Rest Framework 'Manager' 对象不可下标

Django 1.8 数组字段追加和扩展

Django:覆盖每个应用程序而不是每个项目的“不可覆盖”管理模板?

django项目模型字段

无法在 django 中解压不可迭代的 ModelBase 对象

如何在搜索栏中按布尔字段过滤 - django