# django-restframework-test_1 **Repository Path**: mjinnn/django-restframework-test_1 ## Basic Information - **Project Name**: django-restframework-test_1 - **Description**: django restframework test - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 23 - **Created**: 2023-02-05 - **Last Updated**: 2023-02-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Coding challenge - API We'd like to extend this Django REST Framework project (based on the one found [here](https://github.com/beheshtraya/django_rest_sample)). The API stores information about authors, categories and books. We'd like to let our API users view library statistics, namely: 1. Total number of books owned. 2. Total cost of all books. 3. Author with most books. 4. Category with most books. There are a few ways to approach this; feel free to choose one that seems appropriate, and please explain why you think it's best for us. We've also heard of at least one bug in the codebase. Can you try to identify issues in the existing code, and document how you discovered and fixed the bugs? Also: - Please treat all code as production level code. - Please don’t spend more than 2 days on this. If time runs short, an explanation of what you would do given more time is fine. - Write an Dockerfile to deploy it with Docker into production env. (Optional) - Please send us the pull request link of your changes for the submission. (Important) ## Original `readme.txt` > pip install -r requirements.txt > python manage.py makemigrations > python manage.py migrate > python manage.py runserver -- server will be run on 127.0.0.1:8000 ## 优化总结: 1. model中的__str__函数必须返回str类型。如果为None,至少admin会报错。 2. model加上注释,方便其他同事接手或者方便自己以后回忆。 3. 价格相关使用FloatField类型时,计算过程中可能会存在精度问题,我习惯使用IntegerField,单位为分。或者有更好的解决方案也可以选用 4. 格式化代码(可读性更高,另外看着难受) 5. requirements.txt中的依赖,固定版本号,避免部分依赖版本升级导致的兼容问题 ## 统计接口解释: 尽量的利用聚合函数在数据库层处理好所需的数据,减少连接数据库的次数,因为每次连接数据库都需要时间成本,在处理大量的数据时耗时相对更少, 用户等到时间更短。 ## 如何发现bug: 发现问题:将项目跑起来,报错提示:name 'CategorySerializer' is not defined。第一个bug浮出水面 解决方案:CategorySerializer类放在BookReadSerializer前面即可 发现问题:运行admin,点开书籍详情有报错,根据报错得知__str__这个函数返回存在None值。 解决方案:判断一下返回的字段是否为None或者title字段改为null=False即可 ## docker部署 打包基础镜像,耗时比较长 > docker build -t django_rest_framework_base:1.0 -f Dockerfile_base ./ 通过docker-compose 后台启动项目 > docker-compose up -d 这里没有写启动是自动执行 python manage.py migrate 命令,需要进容器手动执行一下