用 PostgreSQL 取代 SQLite 作为 Django 模型数据库
为什么使用 PostgreSQL?
- 比SQLite更时尚(虽然我个人比较喜欢SQLite,但是能满足我个人的存储需求,关键是还可以随身携带,可以随时复制,我曾经花19万买了数据,放一下就可以了)放在SQLite身上并将其放在 USB 闪存驱动器上并随身携带......不要跟着我);
- 这更像是在开发而不是在玩;
- 这个项目终于要启动了,为什么不现在开始呢? ,仅使用 PostgreSQL;
- 好吧,我明白为什么SQLite不使用它,那为什么不使用MySQL呢? ——由于我的开发环境使用的是WSL(Windows Subsystem of Linux),目前还无法安装MySQL。
为什么使用 WSL
- 您不必这样做。
- 但是如果您还没有尝试过 WSL,您也可以。 Windows 10是最好的桌面系统(我不接受争论,我用过Mac、不下十个Linux桌面系统,以及其他微软系统,我得出的结论是Windows 10是最好的桌面系统),所以推荐。在Windows 10中完成所有功能。一切都在Windows 10中完成。在这个项目中,除了最终在服务器上的部署之外,一切都可以在Windows中完成。
- 最终的部署环境是Ubuntu环境,所以从现在开始,使用Ubuntu也不错。
- WSL Ubuntu 几乎拥有 Ubuntu 的所有功能(至少是本项目中可以使用的几乎所有功能),并且与“宿主”机器交互良好。例如,文件系统几乎是可互操作的。您可以在 Ubuntu 中访问 Windows 上的文件,这是虚拟机无法实现的。
- WSL 是一个应用程序。环境被你破坏了之后,你得卸载重装,比重装物理机方便多了。
如何安装WSL
超出了本文的范围,请前往Bing。很简单,只需在启动功能中启用子系统功能,然后在Windows Store中安装Ubuntu即可。
安装 PostgreSQL
我在 WSL Ubuntu 上安装了 PostgreSQL,还在 Fedora23 计算机上安装了 PostgreSQL。过程是一样的。下面以Ubuntu为例,如何创建一个新的应用程序(我在之前的文章中已经写过,不再重复解释)。编辑mblog/models.py如下:
"""
描述了一些数据模型,如用户信息、用户写的文章、用户对文章的评论、用户对评论的回复、用户的互粉关系。
虽然这个模型只是用来测试数据库连接的,但场景还是蛮真实的。
"""
from django.db import models
class BlogUsers(models.Model):
"""
Blog users info
"""
user_name = models.CharField(max_length=20)
user_password = models.CharField(max_length=20)
count_create_time = models.DateTimeField(auto_now_add=True)
release_datetime = models.DateTimeField()
class BlogArticles(models.Model):
# use the django.db.models.ForeignKey class to provide a many-to-one relationship
# many articles could belong to one user
author = models.ForeignKey(
BlogUsers,
related_name='articles_author',
# once the user is deleted all the articles that belong to this user to be
# deleted too
on_delete=models.CASCADE # on_delete 描述主表记录被删除后,对此条记录的操作,models.CASCADE表示跟随删除
) # 一个作者可以有多篇文章;当作者被注销了,文章也会被删除
article_title = models.CharField(max_length=200)
article_content = models.CharField(max_length=20000)
create_datetime = models.DateTimeField(auto_now_add=True)
release_datetime = models.DateTimeField()
class ArticleComments(models.Model):
author = models.ForeignKey(
BlogUsers,
related_name='comments_author',
on_delete=models.DO_NOTHING,
)
article = models.ForeignKey(
BlogArticles,
related_name='comments_article',
on_delete=models.DO_NOTHING, # models.DO_NOTHING表示主表记录删除后,对此条记录什么都不做
) # 当作者被注销了,评论还保留下来
# 用parent_comment表示多级评论,‘sele’用以自我关联(即上级评论也是评论类,或者上上级评论也在同一个表中)
parent_comment = models.ForeignKey('self', related_name='replied_comment', null=True, blank=True, on_delete=models.CASCADE)
content = models.CharField(max_length=1000)
class SocialLink(models.Model):
a_user = models.ForeignKey(
BlogUsers,
related_name='a_uer',
on_delete=models.DO_NOTHING,
)
b_user = models.ForeignKey(
BlogUsers,
related_name='b_user',
on_delete=models.DO_NOTHING,
)
is_fans = models.BooleanField(default=False) # 表示a用户对b用户是否关注
Django数据迁移
$ python manage.py makemigrations mblog
$ python manage.py migrate
使用DjangoModel为表格添加内容
$ python manage.py shell
>>> from mblog.models import BlogUsers, BlogArticles, ArticleComments, SocialLink, CommentReply
>>> from datetime import datetime
>>> from django.utils import timezone
>>> datetime_ = timezone.make_aware(datetime.now(), timezone.get_current_timezone())
# 添加两个博客用户。
>>> dinglei = BlogUsers(user_name='丁磊', user_password='163good', release_datetime=datetime.now())
>>> huateng = BlogUsers(user_name='马化腾', user_password='qqgreat', release_datetime=datetime.now())
>>> dinglei.save()
>>> huateng.save()
# 丁磊与马化腾互粉
>>> SocialLink(a_user=dinglei, b_user=huateng, is_fans=True).save()
>>> SocialLink(a_user=huateng, b_user=dinglei, is_fans=True).save()
# 丁磊发表了一篇博文
>>> article = BlogArticles(author=dinglei, article_title='网易游戏真好玩', article_content='不氪金你怎么能变强,氪金也不一定变强', release_datetime=datetime_)
>>> article.save()
# 马化腾评论了文章
>>> comment = ArticleComments(author=huateng, article=article, content='我有王者荣耀')
>>> comment.save()
>>>
>>> dinglei_reply = ArticleComments(author=dinglei, article=article, parent_comment=huateng_comment, content='倩女幽魂,国民手游啊')
>>> dinglei_reply.save()
>>>
>>> huateng_reply = ArticleComments(author=huateng, article=article, parent_comment=dinglei_reply, content='我有王者荣耀')
>>> huateng_reply.save()
>>>
>>> dinglei_reply = ArticleComments(author=dinglei, article=article, parent_comment=huateng_reply, content='阴阳师 平安京,和风moba你有吗?')
>>> dinglei_reply.save()
>>>
>>> huateng_reply = ArticleComments(author=huateng, article=article, parent_comment=dinglei_reply, content='我有王者荣耀')
>>> huateng_reply.save()
>>>
>>> dinglei_reply = ArticleComments(author=dinglei, article=article, parent_comment=huateng_reply, content='我。。。')
>>> dinglei_reply.save()
>>>
>>> huateng_reply = ArticleComments(author=huateng, article=article, parent_comment=dinglei_reply, content='我有王者荣耀')
>>> huateng_reply.save()
最后可以使用图形界面查看数据库中的数据
版权声明
本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。