【问题标题】:Manager isn't accessible via PermiLayer instances error in django无法通过 django 中的 PermiLayer 实例错误访问管理器
【发布时间】:2011-09-24 02:25:46
【问题描述】:

为什么只有 PermiLayer 模型会出现这样的错误,而其他模型也可以正常工作。

我的代码是这样的

 uperm = PermiLayer()
 perml=uperm.objects.get(pk = num)

Ps:我已经尝试过Manager isn't accessible via `Model` instances那里的解决方案。但是没有;没有成功

编辑:

这是我的模型

from django.contrib.gis.db import models
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from maps.models import  Sdr_Layer
from OsmMap.models import OsmLayers
class PermiLayer(models.Model):
    user = models.ForeignKey(User)
    table = models.IntegerField()
    permi = models.IntegerField()

【问题讨论】:

标签: django


【解决方案1】:

管理器只能通过模型​​类而不是模型实例访问 https://docs.djangoproject.com/en/dev/topics/db/queries/#retrieving-objects

你为什么不喜欢它呢

perml=PermiLayer.objects.get(pk = num)

【讨论】:

  • PermiLayer 匹配查询不存在。我认为这是一个错误。
  • num的类型是什么?希望不是字符串。你也确定你有这样的行吗?
  • 这是一个字符串。将其类型转换为 int 。但仍然面临同样的问题
  • 我认为你的 pk 没有这样的对象。这就是引发 DoesNotExist 错误的方式。
  • 嗯。我认为这更像是我如何设计模型而不是代码错误的逻辑错误。
猜你喜欢
  • 2021-10-08
  • 1970-01-01
  • 1970-01-01
  • 2011-07-29
  • 2011-04-21
  • 1970-01-01
  • 2022-06-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多