Python Django不在数据库中创建对象
我不知道怎么了。 我已经尝试过迁移/makemigrations,但没有帮助,我在virtualenv tho中工作。 我的模特Python Django不在数据库中创建对象,python,django,postgresql,Python,Django,Postgresql,我不知道怎么了。 我已经尝试过迁移/makemigrations,但没有帮助,我在virtualenv tho中工作。 我的模特 from django.db import models import re # Create your models here. class Currency(models.Model): def __str__(self): return self.short name = models.CharField(max_le
from django.db import models
import re
# Create your models here.
class Currency(models.Model):
def __str__(self):
return self.short
name = models.CharField(max_length=32, null=True, blank=True)
country = models.CharField(max_length=50, null=True, blank=True)
short = models.CharField(max_length=10, null=True, blank=True)
class Source(models.Model):
def __str__(self):
return self.name + '({}...)'.format(self.url[:20])
url = models.URLField()
name = models.CharField(max_length=250, verbose_name='Source_name')
timestamp = models.DateTimeField(auto_now_add=True)
currency = models.ForeignKey(Currency)
def save(self, *args, **kwargs):
self.name = ''.join(re.sub('\s+', ' ', self.name).strip())
super(Source, self).save()
class Product(models.Model):
def __str__(self):
return self.name
name = models.CharField(max_length=250, blank=False)
prices = models.ManyToManyField(to='Price', blank=True)
date_creation = models.DateField(auto_now_add=True)
prod_code = models.CharField(max_length=250, blank=True, null=True)
def save(self, *args, **kwargs):
self.name = ''.join(re.sub('\s+', ' ', self.name).strip())
super(Product, self).save()
我创建对象的函数
def from_csv_to_db(csv_name): # empty DB
try:
df = pd.read_csv(csv_name, sep=';')
except FileNotFoundError:
return ('Please input correct path to csv file or check spelling. Process finished')
for i, row in df.iterrows():
Currency.objects.get_or_create(short=row['source__currency__short'], id=row['source__currency_id'])
Product.objects.get_or_create(id=row['product__id'], name=row['product__name'])
Source.objects.get_or_create(id=row['source__id'], name=row['source__name'], url = row['source__url'],
currency_id=row['source__currency_id'])
Parser.objects.get_or_create(script=row['script'], boolean=row['boolean'], product_id=row['product__id'],
source_id=row['source__id'])
return 'Done'
然而,django确实通过queryset显示我的模型对象
在python manage.py shell中
但是,当我运行python manage.py dbshell>select*from Parser_product;
我得到错误:关系解析器\产品不存在
我在Debian上使用Docker,这是远程完成的。尝试运行makemigration命令和应用程序名称。如果默认应用程序中有模型,则需要此选项。例如,python manage.py makemigrations解析器。然后运行migrate命令。我很高兴通过python聊天电报找到了解决方案 答案将是一个原始的问题 我必须让它选择*FROM Parser_product<注意引号
多亏了这个您好,您运行过python manage.py makemigrations和python manage.py migrate吗?编辑:我看到您这样做了,这些命令的输出是什么?编辑:输出是:对于makemigrations-未检测到任何更改migrate-未应用任何迁移。我甚至尝试指定要迁移的内容,即解析器模型,但都不起作用。这是因为Django在表名前面加了appname,我想你已经将你的应用添加到“settings.py”中的“INSTALLED_APPS”了吗?看起来django没有创建表yetTables是否存在python manage.py dbshell显示它们,但我得到一个错误:当我尝试访问表时,关系解析器_price不存在。是的,这是在已安装的应用程序中:/AFAIU,它更像是原始查询的问题,而不是迁移命名问题。我提到了这一点,正如nexla在他的一条评论中所说,他无法进行迁移。我之所以不能,是因为我已经让它运行了,而且按照您所说的方式,问题是@floatingpurr提到的原始查询
from Parser.models import *
Product.objects.all()
<QuerySet [<Product: test product>, <Product: iphone>, <Product:
Mac>, <Product: iPad>]>