ManyToManyfield生成了错误的模型 [英] ManyToManyField makes an incorrect model
本文介绍了ManyToManyfield生成了错误的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮帮我
我的模型.py
# Create your models here.
class Information(models.Model):
id = models.CharField(max_length=200, primary_key=True)
title = models.CharField(max_length=500)
link = models.CharField(max_length=100)
summary = models.CharField(max_length=1000)
published = models.CharField(max_length = 100)
def __str__(self):
return self.title
class Search(models.Model):
id = models.CharField(max_length=100, primary_key=True)
searched_titles = models.CharField(max_length=100)
searched_topics = models.CharField(max_length=100)
number_found_articles = models.IntegerField()
def __str__(self):
return self.id
class Article_search(models.Model):
id = models.CharField(max_length=100, primary_key=True)
found_articles = models.ManyToManyField(
Information)
search_details = models.ManyToManyField(
Search)
在我的MySQL工作台中,我可以看到表CONTACT_SEARCH已被填充。 但 当我转到我的Django项目的管理页面时,我也看到了不同的对象,当我试图阅读它们时,我得到了一个奇怪的错误: 表‘kcbbe2.database_article_search_found_articles’不存在&Quot;,‘42S02’)
我添加了一些图片 picture of the admin page in django 因此,为了清楚起见,当我点击图片中的对象时,我会得到错误。我在设置模型时做错了什么?要添加的#
在pythonscript中,我填充数据库。该脚本如下所示:
# Query to create table if this one doesn't exists yet
create_search_table_query = """
CREATE TABLE IF NOT EXISTS database_Article_search(
id int not null auto_increment,
found_articles VARCHAR (500),
search_details VARCHAR (100),
primary KEY (id),
FOREIGN KEY (found_articles) REFERENCES database_Information(id),
FOREIGN KEY (search_details) REFERENCES database_Search(id))
"""
# Execute query to create table
with connection.cursor() as cursor:
cursor.execute(create_search_table_query)
connection.commit()
# get cursor object
cursor = connection.cursor()
# For every found article, the article id and search_deatials will be put in the database
for article in titles:
sql = 'INSERT INTO database_Article_search (found_articles, search_details) VALUES ("' + article.id + '", "' + str(id.int) + '")'
cursor.execute(sql)
connection.commit()
推荐答案
您能检查一下哪个数据库当前正在使用SQLite或MySql吗?
这篇关于ManyToManyfield生成了错误的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文