想要替换之前的代码objects.get(),首先先找到项目中哪些地方用到了objects.get()
命令:grep -rnw objects.get ./
#在终端输入命令
luciano@luciano:~/Desktops/$ grep -rnw objects.get ./
./vip/models.py:25: perm = Permission.objects.get(name = perm_name)
./lib/orm.py:18: model_obj = cls.objects.get(*args,**kwargs)
./scripts/init.py:110: vip1 = Vip.objects.get(level = 1)
./scripts/init.py:111: vip2 = Vip.objects.get(level = 2)
./scripts/init.py:112: vip3 = Vip.objects.get(level=3)
./scripts/init.py:115: vipflag = Permission.objects.get(name='vipflag')
./scripts/init.py:116: superlike = Permission.objects.get(name='superlike')
./scripts/init.py:117: rewind = Permission.objects.get(name='rewind')
./scripts/init.py:118: anylocation = Permission.objects.get(name='anylocation')
./scripts/init.py:119: unlimit_like = Permission.objects.get(name='unlimit_like')
./scripts/init.py:120: show_like_me = Permission.objects.get(name='show_like_me')
./common/middleware.py:36: user = User.objects.get(pk=uid)
./App/models.py:56: self._vip = Vip.objects.get(id = self.vip_id) # user.vip_id对应的是Vip表的primery_key(id)所以vip_id default = 1对应Vip表中的level是0
使用流编辑器sed
sed 's/objects.get/get/g' scripts/init.py
sed -i 's/objects.get/get/g' scripts/init.py
luciano@luciano:~/Desktops/$ sed 's/objects.get/get/g' scripts/init.py
# 终端显示被修改后的代码实例,此时还没有修改
luciano@luciano:~/Desktops/$ sed -i 's/objects.get/get/g' scripts/init.py
# 完全修改
# 解释
# sed 's' -->替换模式
# sed 's /被替代码/替换代码/'
# sed 's /被替代码/替换代码/g' -->global 全文替换
# sed 's /被替代码/替换代码/g' 路径/文件名.py
# sed -i 's /被替代码/替换代码/g' 路径/文件名.py : --> 修改