【问题标题】:app name is appended to the table name in django应用程序名称附加到 django 中的表名称
【发布时间】:2017-11-17 16:34:58
【问题描述】:

当我尝试从表中获取数据时,应用程序名称会附加到表名称并显示错误。以下是我的代码。

from models import open_cart

class test(APIView):

  def get(self,request,format=None):
    values = open_cart.objects.get()

我在 installed_apps 中定义的应用名称是“MyApp”。我的表名称是“open_cart”。查询中的表名称为 MyApp_open_cart 而不是 open_cart。 我收到的错误消息是 relation "untitled_open_cart" 不存在

【问题讨论】:

  • 发布您的模型以及设置文件

标签: django postgresql django-models django-rest-framework


【解决方案1】:

将应用程序名称附加到表名称是 Django 中的默认行为。如果您想使用自定义表名,请将其添加到Meta class。

class MyModel(models.Model):
    class Meta(CommonInfo.Meta):
        db_table = 'student_info'

请参阅official Django Model Meta options documentation 了解更多信息。

【讨论】:

    【解决方案2】:

    Django 默认在模型名称前附加应用名称。如果我们想使用我们的自定义表名,那么它必须在 Meta 类中提及。

    class Open_cart(models.Model):
        class Meta:
            db_table = 'open_cart'
    

    【讨论】:

      猜你喜欢
      • 2013-10-28
      • 2011-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多