我一直在python中使用pandas,我通常会将数据帧写入我的db表,如下所示.我现在正在迁移到Django,如何通过名为MyModel的模型将相同的数据帧写入表中?援助真的很感激.
# Original pandas code
engine = create_engine('postgresql://myuser:mypassword@localhost:5432/mydb', echo=False)
mydataframe.to_sql('mytable', engine,if_exists='append',index=True)
Run Code Online (Sandbox Code Playgroud) 我有一个ms访问表,跟踪50个产品及其每日销售量.我想使用vba 1 csv文件(包括标题)导出每个产品,显示记录集中的每日卷而不将记录集保存到永久查询.我正在使用下面的代码,但我被困在代码中下面突出显示的实际导出点.任何帮助解决这个问题表示赞赏.
Dim rst As Recordset
Dim rstId As Recordset
SQLExportIds = "SELECT DISTINCT tblDailyVols.SecId FROM tblDailyVols WHERE tblDailyVols.IsDeleted=False"
Set rstId = CurrentDb.OpenRecordset(SQLExportIds)
If rstId.EOF = True Then
MsgBox "No Products Found"
Exit Sub
End If
Do While rstId.EOF = False
SecId = rstId.Fields("SecId")
SQLExportQuotes = " SELECT tblDailyVols.ID , tblDailyVols.TradedVolume, tblDailyVols.EffectiveDate FROM tblDailyVols "
SQLExportQuotes = SQLExportQuotes & " WHERE tblDailyVols.IsDeleted=False and tblDailyVols.ID = " & SecId
SQLExportQuotes = SQLExportQuotes & " ORDER BY tblDailyVols.EffectiveDate "
Set rst …Run Code Online (Sandbox Code Playgroud) 我在PostgreSQL上有一个遗留数据库,有一个简单的产品表,有2列(productid,productname),有70个产品.我正在尝试使用Django作为前端,我有下面的模型和模型表单.保存过程失败,出现以下错误.
duplicate key value violates unique constraint "productidentifier"
DETAIL: Key (productid)=(4) already exists.
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助纠正错误.我已在Postgres表上确认索引获取序列设置正确,期望值应为72.请参见下面的模型,表单和视图.
#models.py
class Products(models.Model):
productid = models.AutoField(primary_key=True)
productname = models.TextField(verbose_name='Product Name')
class Meta:
managed = False
#Views.py
def new_product(request):
if request.method == 'POST':
form= forms.EditProductForm(request.POST)
if form.is_valid():
product = form.save()
return redirect('views.full_product_list')
else:
form = forms.EditProductForm()
return render(request, 'edit_product.html', {'form': form})
#forms.py
class EditProductForm(forms.ModelForm):
productname = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}))
class Meta:
model = models.Products
fields = ('productname')
Run Code Online (Sandbox Code Playgroud) 我正在尝试对相关对象进行以下过滤的不同项目的SQL Alchemy查询,其等效于以下查询:
SELECT distinct items.item_id, items.item_name
FROM items
INNER JOIN categories as cat on items.category_id = cat.category_id
INNER JOIN stores on cat.store_id = stores.store_id
WHERE store.store_id = 123
Run Code Online (Sandbox Code Playgroud)
我已经创建了以下包含外键的模型,但是当我在下面运行查询时,它不能正确过滤。
items_query = (db.session.query(Store, Item)
.filter(Store.store_id == 123)
).all()
#SQL Alchemy Models
class Store(db.Model):
__tablename__ = 'stores'
store_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
store_name = db.Column(db.String(100), unique=True, nullable=False)
def __repr__(self):
return '<Store>'+str(self.store_name)
class Category(db.Model):
__tablename__ = 'categories'
category_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
category_name = db.Column(db.String(100), unique=True, nullable=False)
store_id = db.Column(db.Integer, db.ForeignKey('stores.store_id'))
store = …Run Code Online (Sandbox Code Playgroud) django ×2
postgresql ×2
python ×2
access-vba ×1
dataframe ×1
distinct ×1
django-forms ×1
modelform ×1
ms-access ×1
pandas ×1
sqlalchemy ×1