Code前端首页关于Code前端联系我们

Django 构建 RESTful Web 服务:使用 PostgreSQL 而不是 SQLite

terry 2年前 (2023-09-24) 阅读数 53 #后端开发

为什么使用 PostgreSQL?

  • 比SQLite更时尚(虽然我自己很喜欢SQLite,但它满足了我大部分个人存储需求。最重要的是它还可以随身携带,可以随时复制。我曾经花19万元买过数据。只是把它放在SQLite中,然后把它放在U盘上的记忆棒上,然后随身携带……别跟我学);
  • 这更像是开发,而不仅仅是玩耍;
  • 该项目最终会上线,那为什么不现在就开始呢? ,仅使用 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。过程类似。下面是一个名为 mblog 应用程序的 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()

最后可以使用图形用户界面查看数据库中的数据Django构建RESTful网络服务:使用PostgreSQL替代SQLite

版权声明

本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门