【问题标题】:'NoneType' object has no attribute 'negate' [duplicate]“NoneType”对象没有属性“否定”[重复]
【发布时间】:2022-01-25 14:36:09
【问题描述】:

我在尝试获取限制字段为 True 的所有 Url 时遇到错误。我已尝试删除迁移并创建新迁移,但仍然遇到相同的错误。

这些是已安装的依赖项:

  • asgiref==3.4.1
  • backports.zoneinfo==0.2.1
  • Django==4.0
  • django-cors-headers==3.10.1
  • django-shell-plus==1.1.7
  • djangorestframework==3.13.1
  • djongo==1.3.6
  • dnspython==2.1.0
  • gunicorn==20.1.0
  • pymongo==3.12.1
  • python-dotenv==0.19.2
  • pytz==2021.3
  • sqlparse==0.2.4
  • tzdata==2021.5

models.py:

class Urls(models.Model):
    _id = models.ObjectIdField()
    record_id = models.IntegerField(unique=True)
    hash = models.CharField(unique=True, max_length=1000)
    long_url = models.URLField()
    created_at = models.DateField(auto_now_add=True)
    expires_in = models.DurationField(default=timedelta(days=365*3))
    expires_on = models.DateField(null=True, blank=True)
    limited = models.BooleanField()
    exhausted = models.BooleanField()

查询:

Urls.objects.filter(limited=False)

错误:

Traceback (most recent call last):
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 857, in parse
    return handler(self, statement)
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 933, in _select
    return SelectQuery(self.db, self.connection_properties, sm, self._params)
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 116, in __init__
    super().__init__(*args)
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 62, in __init__
    self.parse()
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 152, in parse
    self.where = WhereConverter(self, statement)
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/djongo/sql2mongo/converters.py", line 27, in __init__
    self.parse()
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/djongo/sql2mongo/converters.py", line 119, in parse
    self.op = WhereOp(
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/djongo/sql2mongo/operators.py", line 476, in __init__
    self.evaluate()
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/djongo/sql2mongo/operators.py", line 465, in evaluate
    op.evaluate()
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/djongo/sql2mongo/operators.py", line 258, in evaluate
    self.rhs.negate()
AttributeError: 'NoneType' object has no attribute 'negate'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/djongo/cursor.py", line 51, in execute
    self.result = Query(
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 784, in __init__
    self._query = self.parse()
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 885, in parse
    raise exe from e
djongo.exceptions.SQLDecodeError:

        Keyword: None
        Sub SQL: None
        FAILED SQL: SELECT "short_urls"."_id", "short_urls"."record_id", "short_urls"."hash", "short_urls"."long_url", "short_urls"."created_at", "short_urls"."expires_in", "short_urls"."expires_on", "short_urls"."limited", "short_urls"."exhausted" FROM "short_urls" WHERE NOT "short_urls"."limited" LIMIT 21
        Params: ()
        Version: 1.3.6

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/djongo/cursor.py", line 59, in execute
    raise db_exe from e
djongo.database.DatabaseError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/django/db/models/query.py", line 256, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/django/db/models/query.py", line 262, in __len__
    self._fetch_all()
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/django/db/models/query.py", line 1354, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/django/db/models/query.py", line 51, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1202, in execute_sql
    cursor.execute(sql, params)
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 99, in execute
    return super().execute(sql, params)
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/mnt/c/Users/pranjal/Desktop/urlhash/env/lib/python3.8/site-packages/djongo/cursor.py", line 59, in execute
    raise db_exe from e
django.db.utils.DatabaseError

【问题讨论】:

  • 你写的limited=False
  • 应该是 Urls.objects.all().filter(limitted = True)
  • 问题,@RitankarBhattacharjee,Urls.objects.all().filter(limited=True)Urls.objects.filter(limited=True) 不一样吗?但我同意,这个问题确实表明查询与 OP 在查询中写的相反。
  • @R.Uziel,是的,是的,实际上我习惯于写.all(),但主要是我指出有限的灵魂是平等的

标签: python-3.x django mongodb mongoengine djongo


【解决方案1】:

我需要查看你在哪里进行查询的 views.py,Urls.objects.filter(limited=False) 但首先,查询可能没有找到任何符合条件的 Urls 对象,limited=False,因此查询返回 None,它有没有属性。其次,我不熟悉否定。我没有在你的模型中看到它。也许如果我看到你进行查询的views.py,我会更好地理解。

【讨论】:

  • 没错,views.py 会明确否定的来源
猜你喜欢
  • 2016-09-20
  • 2019-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-01
  • 2021-05-26
  • 2023-03-28
相关资源
最近更新 更多