【问题标题】:check to check DB tables exists in Django?检查以检查 Django 中是否存在数据库表?
【发布时间】:2021-05-24 01:14:43
【问题描述】:

我为产品写了这个表格,里面有所有品牌,然后为品牌输入设置选择列表

class ProductForm(forms.ModelForm):

class Meta:
    BRAND_CHOICE = Brand.objects.all()
    model = Product
    fields = '__all__'
    widgets = {
        'brand': forms.Select(choices=BRAND_CHOICE),
     }

但运行时出错

python manage.py 迁移

我犯的一个错误

django.db.utils.OperationalError: no such table: app_product_brand

那么我如何检查数据库,如果表存在然后查询数据库?

【问题讨论】:

  • 在运行 migrate 之前是否运行了python manage.py makemigrations

标签: django database django-queryset


【解决方案1】:

如果你有这个模型,你可以检查你的 models.py app_product_brand ,如果你已经有做

python manage.py makemigrations
python manage.py migrate app_product_brand

如果没有,您可以使用此检查所有表

from django.db import connection
all_tables = connection.introspection.table_names()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-13
    • 2017-03-31
    • 2013-05-11
    相关资源
    最近更新 更多