【发布时间】:2021-10-17 13:17:24
【问题描述】:
我正在尝试从 Django 管理页面中删除存储在数据库中的对象,但出现错误 django.db.utils.ProgrammingError: (1146, "Table 'lab_equipment.lab_add' 不存在")
观看次数
from django.shortcuts import render, redirect, get_object_or_404
from django.http import JsonResponse, HttpResponseRedirect
from django.urls import reverse
from .forms import *
from .models import *
# Create your views here.
def main(request):
return render(request, 'Lab/Lab.html')
def generateView(request):
if request.method == 'POST': # If the form has been submitted...
esrl = Esrl_form(request.POST) # Create a form instance
equ = equipment(request.POST)
num = count_form(request.POST)
add = Add_form(request.POST)
if esrl.is_valid() and equ.is_valid() and num.is_valid() and add.is_valid(): # All validation rules pass
print("all validation passed")
loc = esrl.save()
e = equ.save(commit=False)
c = num.save(commit=False)
a = add.save(commit=False) # not to save and connect the Foreign key
e.location_number = loc # b.foreignkeytoA = a Connecting foreign key
e.save()
c.equipment_name = e
c.loc_num = loc
c.save()
a.location_number = loc
a.equipment_name = e
a.stock = c
a.save()
return HttpResponseRedirect(reverse('home'))
else:
print("failed")
else: # if the request method is not post then create a form instance u if the request method is not post then create a form instance and loaded in the templatenloaded in the template
esrl = Esrl_form
equ = equipment
num = count_form
add = Add_form
return render(request, 'Lab/Add.html', { # Sending multiple forms to the HTML page
'esrl': esrl,
'equ': equ,
'num': num,
'add':add,
})
型号
from django.db import models
import uuid
# Create your models here.
class Esrl(models.Model):
location_number = models.CharField(
max_length=15)
def __str__(self):
return self.location_number
class ESRLequipmentinfo(models.Model):
equipment_name = models.CharField(max_length=40)
location_number = models.ForeignKey(Esrl, on_delete=models.CASCADE)
created_date = models.DateTimeField(auto_now_add=True, null=True)
def __str__(self):
return self.equipment_name
class count(models.Model):
stock = models.PositiveIntegerField()
equipment_name = models.ForeignKey(
ESRLequipmentinfo, on_delete=models.CASCADE)
loc_num = models.ForeignKey(Esrl, on_delete=models.CASCADE)
def __str__(self):
string = str(self.stock)
return str(self.loc_num) ,string, self.equipment_name
class move(models.Model):
name = models.CharField(max_length=150)
created_date = models.DateTimeField(auto_now_add=True, null=True)
location_number = models.ForeignKey(Esrl, on_delete=models.CASCADE)
equipment_name = models.ForeignKey(
ESRLequipmentinfo, on_delete=models.CASCADE)
stock = models.ForeignKey(count, on_delete=models.CASCADE)
class add(models.Model):
name = models.CharField(max_length=150)
created_date = models.DateTimeField(auto_now_add=True, null=True)
location_number = models.ForeignKey(Esrl, on_delete=models.CASCADE)
equipment_name = models.ForeignKey(
ESRLequipmentinfo, on_delete=models.CASCADE)
stock = models.ForeignKey(count, on_delete=models.CASCADE)
class History(models.Model):
name = models.CharField(max_length=150)
Previous_location = models.CharField(max_length=40)
new_location = models.CharField(max_length=40)
euipment_name = models.CharField(max_length=40)
equipment_count = models.IntegerField()
created_date = models.DateTimeField(auto_now_add=True)
表格
from django import forms
from .models import *
class Esrl_form(forms.ModelForm):
class Meta:
model = Esrl
fields = '__all__'
template_name = 'Lab/Add.html'
class equipment(forms.ModelForm):
class Meta:
model = ESRLequipmentinfo # Esrl,count
fields = ('equipment_name',)
template_name = 'Lab/Add.html'
# fields = '__all__'
class count_form(forms.ModelForm):
class Meta:
model = count
fields = ('stock',)
template_name = 'Lab/Add.html'
# fields = '__all__'
class Add_form(forms.ModelForm):
class Meta:
model = add
fields = ('name',)
template_name = 'Lab/Add.html'
设置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'Lab_equipment',
'USER': '###',
'PASSWORD': '###',
'HOST': 'localhost',
'PORT': '3306',
}
}
- 我尝试注释代码并运行 python manage.py makemigrations python manage.py migrate -fake 但这不起作用
- 尝试使用
python manage.py dbshell
这给了我一个错误 CommandError:您似乎没有安装“mysql”程序或在您的路径上。 我使用的是 Mac 操作系统,我 pip3 在我的虚拟环境中安装了 MySQL-python、pymysql、mysqlclient,并且在本地运行 which mysql 时出现错误 mysql not found。 另外,我已经授予了 mysql 的所有权限。
模块
Django==3.2.6,
django-bootstrap-form==3.4,
mysql==0.0.3,
mysql-client==0.0.1,
mysql-connector-python==8.0.26,
mysqlclient==2.0.3,
PyMySQL==1.0.2,
sqlparse==0.4.1,
toml==0.10.2.
【问题讨论】:
-
该错误告诉您尚未运行迁移。这意味着:您在 models.py 中的 python 代码与您的 DB-Structure 不匹配。您需要运行 makemigrations your_app 来解决问题。
-
运行
python manage.py makemigrations和python manage.py migrate将模型更改应用到实际数据库 -
我在运行
python manage.py makemigrations AppName后导致“在应用程序中未检测到任何更改”。python manage.py migrate给了我“没有要申请的迁移”。我试图从数据库中删除对象并得到相同的结果。
标签: python django django-models