Django - 向 MySQL 数据库添加行

2024-04-15数据库问题
2

本文介绍了Django - 向 MySQL 数据库添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

所以我已经有一个数据库设置,其中已经插入了几列和几行.我正在尝试创建一个视图,您只需将信息输入表单并按提交,然后将添加一行包含您刚刚输入的信息的 MySQL 数据库.

So I already have a database setup with a few columns and a few rows already inserted in. I'm trying to create a view that you would just input information into a form and press Submit, then a row would be added to the MySQL database with the information you just typed in.

我相信您可以使用 admin 执行此操作,但我想在没有 admin 的情况下尝试,我不确定这是否可行?到目前为止,我一直在使用 MySQL 命令行添加行..

I believe you can do this with admin, but I would like to try without admin and I'm not sure if this is possible? I've been using the MySQL commandline to add rows as of now..

推荐答案

当然这是可能的,这是数据驱动网站的构建块.您可以使用 Daniel 建议的 ModelForm(他们免费提供内置验证和 HTML 标记)来轻松地将您的模型映射到前端表单.首先从 django 教程或文档开始可能会有所帮助.

Of coures this is possible this is a building block for data driven websites. You can use a ModelForm as Daniel suggested (they offer built in validation and HTML markup for FREE) to easily map your model to a front end form. It would probably be beneficial to start with django tutorial or documentation first.

最基本的,你所要做的就是实例化你的模型

At the the very basic, all you have to do is instantiate your model

new_entry = YourModel(name='me', age='222', about='stackoverflow')

然后保存

new_entry.save()

这会将其作为新行添加到您的数据库中.

This adds it as a new row to your db.

https://docs.djangoproject.com/en/dev/主题/数据库/模型/

这篇关于Django - 向 MySQL 数据库添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Mysql目录里的ibtmp1文件过大造成磁盘占满的解决办法
ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致...
2025-01-02 数据库问题
151

SQL 子句“GROUP BY 1"是什么意思?意思是?
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)...
2024-04-16 数据库问题
62

MySQL groupwise MAX() 返回意外结果
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)...
2024-04-16 数据库问题
13

MySQL SELECT 按组最频繁
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)...
2024-04-16 数据库问题
16

为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同
Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)...
2024-04-16 数据库问题
13

MySQL GROUP BY DateTime +/- 3 秒
MySQL GROUP BY DateTime +/- 3 seconds(MySQL GROUP BY DateTime +/- 3 秒)...
2024-04-16 数据库问题
14