【问题标题】:Relation <Name> doesn't exist when using Model.objects.get() django使用 Model.objects.get() django 时,关系 <Name> 不存在
【发布时间】:2021-09-03 09:09:16
【问题描述】:

历史: 我一直在使用 GNUCash 进行会计,它存储所有客户信息,以便集成文件和发票的作业交付,我正在将 Postgres 上的 GNUCash 数据库与现有的本地服务器集成以发送和备份文件并自动标记它们。

所以我做了inspectdb&gt; models.py 并从 gnucash 数据库中获取了所有模型。 现在 'Customers.objects.all()' 是工作文件并提供所有数据的列表,但 'Customers.objects.get()' 不起作用并给出错误。

查看:

def get_job_list(client_id):
    try:
        client = Customers.objects.get(id = client_id).using('gnucash')
        print(client)
        ###job_list = Jobs.objects.get(owner_guid = client.guid,active = 0).using('gnucash')
    except Exception as e:
        job_list = None

    return job_list

错误:

上述错误表明该表不存在客户。 但是当我将代码更改为Model.objects.all() 时,它工作正常。

查看:

def get_job_list(client_id):
    try:
        client = Customers.objects.all().using('gnucash') #This is Changed
        print(client)
        job_list = None
        ###job_list = Jobs.objects.get(owner_guid = client.guid,active = 0).using('gnucash')
    except Exception as e:
        job_list = None

    return job_list

输出:

我查看了This question,它专注于小写表名,这对我来说是正确的.all() 工作但不是.get()。 如果你想看看,这里是模型:

class Customers(models.Model):
    guid = models.CharField(primary_key=True, max_length=32)
    name = models.CharField(max_length=2048)
    id = models.CharField(max_length=2048)
    notes = models.CharField(max_length=2048)
    active = models.IntegerField()
    .
    .
    .
    class Meta:
        managed = False
        db_table = 'customers'

DB pgAdmin 中的表名:

表格列表:使用\dt

按照@boyenec 的建议在.get 中使用硬编码值:

def get_job_list(client_id):
    try:
        print("Getting Jobs List")
        #client = Customers.objects.filter(id__in=[client_id]).using('gnucash')
        client = Customers.objects.get(id = "CUS000001").using('gnucash')
        #job_list = Jobs.objects.filter(owner_guid__in = [client[0].guid],active__in = [1]).using('gnucash')
    except Exception as e:
        print("Oh No Error !")
        print(e)
        job_list = None
    return job_list

错误输出:

在管理站点注册的应用 ERP: 更新:不要使用此检查解决问题的答案

我开始使用client = Customers.objects.filter(id__in=[client_id]).using('gnucash') 进行查询,因为除了Model.objects.get() 之外的一切似乎都可以正常工作

【问题讨论】:

  • \dtpsql 中返回的数据库中表的名称是什么?
  • @AdrianKlaver 我从 pgAdmin 添加了一张图片。表名是小写的“客户”

标签: python django postgresql django-models


【解决方案1】:

当您使用 objects.get 时,这意味着您正在尝试查找特定对象。您必须包含特定的对象 ID 或值。您不能在 objects.get 中使用 client_id、name 等变量。请参阅django documentation。您需要使用实际的客户端 ID 或名称,例如 1、2、3、“jhone”、“Mike”,而不是使用 client_id 或名称。当你使用这个

client = Customers.objects.get(id = client_id).using('gnucash')

您正在告诉获取 client_id,但您没有告诉哪个 client_id。所以你得到了错误。您需要指定类似这样的客户端 ID

#example1

client = Customers.objects.get(pk=1).using('gnucash')

#example2

 client = Customers.objects.get(name="jhone").using('gnucash') #it will return the customer whose name is jone.

当您使用 get_all() 时,它会返回所有对象,因此您不会收到错误消息,并且过滤器与 objects.get 不同。您可以在过滤器中使用 client_id 之类的变量,但不能使用 objects.get

通常我们使用 objects.get 来查找任何特定对象。假设您有 100 个客户,并且您只想找到属于特定地区或地点的客户,或者我们只想找到女性客户。然后我们将使用objects.get。

#find customers those only belongs from Arizona

     client = Customers.objects.get(place="arizona")

#find only female customers
     client = Customers.objects.get(gender="female")

您可以通过使用 filter 和 objects.get_all() 来做同样的事情,但上面的示例帮助您理解 objects.get()

的概念

【讨论】:

  • 我了解您的意图。不幸的是,这不是问题。我确实用硬编码的“CUS000001”检查了它,它给出了同样的错误。另一方面,如果两种数据类型相同,您可以执行id = client_id ,并且如果您的模型字段是 char 类型,并且如果您想确保执行 str(client_id) 并且如果您的模型字段是整数类型,请执行 int(client_id)。文档显示您需要相同类型的数据进行比较。
  • 我可以看看你的硬编码是为“CUS000001”做的吗?您应该得到没有任何错误的结果。你可能在某个地方做错了
  • 当然,我在更新前添加了所需的视图和输出。
  • 您能否也试试 name="your coustomer name" 并告诉我结果。你能显示一行客户表吗?
  • 转到您的 django 管理面板并复制客户 ID 并粘贴到您的代码中,然后让我知道结果。如果我能看到你的顾客桌子的一排就更好了。
【解决方案2】:

在 django 中使用非托管数据库(非托管或不是由 django 创建)Model.objects.filter().using('gnucash')Model.objects.all().using('gnucash') 似乎可以工作,但 Model.objects.get().using('gnucash') 无效。

正如@boyenec 建议从django 管理面板复制Customers 的值以确保我们拥有正确的值。我知道管理面板只能与数据库路由器一起使用,因为 .using() 没有添加到 admin site

数据库路由器对错误进行了排序,但我不得不参考文档,我查看了文档,发现它不是Model.objects.get().using('gnucash'),而是Model.objects.using('gnucash').get() 它以某种方式适用于过滤器,但适用于get() 它是不工作。

.using('dbname') 放在Model.objects 之后而不是.get() 之后。

Documentation 用于多数据库 django 3.2

【讨论】:

    猜你喜欢
    • 2017-07-05
    • 2019-08-02
    • 2016-01-05
    • 2020-04-09
    • 2014-07-18
    • 2020-12-12
    • 1970-01-01
    • 2020-03-09
    • 2016-03-17
    相关资源
    最近更新 更多