检查 Django 中是否存在数据库表?
check to check DB tables exists in Django?
我为产品写了这个表格,里面有所有的品牌,然后为品牌输入设置选择列表
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 migrate
我犯的一个错误
django.db.utils.OperationalError: no such table: app_product_brand
那么我如何检查数据库,如果表存在然后查询数据库?
您可以检查您的 models.py app_product_brand ,如果您有这个模型,如果您已经有 do
python manage.py makemigrations
python manage.py migrate app_product_brand
如果没有,您可以使用此检查所有表格
from django.db import connection
all_tables = connection.introspection.table_names()
我为产品写了这个表格,里面有所有的品牌,然后为品牌输入设置选择列表
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 migrate
我犯的一个错误
django.db.utils.OperationalError: no such table: app_product_brand
那么我如何检查数据库,如果表存在然后查询数据库?
您可以检查您的 models.py app_product_brand ,如果您有这个模型,如果您已经有 do
python manage.py makemigrations
python manage.py migrate app_product_brand
如果没有,您可以使用此检查所有表格
from django.db import connection
all_tables = connection.introspection.table_names()