1、模型代码:
class Books(models.Model): id = models.CharField(primary_key=True,max_length=32,verbose_name='图书编号') name = models.CharField( max_length=32,verbose_name='图书名称') # author = models.CharField(max_length=24,verbose_name='作者') # pub_date = models.DateTimeField(verbose_name='出版时间') record_status = models.BooleanField(default=False,verbose_name='出借状态') class Meta: db_table = 'SYS_Books' verbose_name = '图书表' def __str__(self): return self.name class Record(models.Model): id = models.CharField(primary_key=True,max_length=32,verbose_name='图书编号') book = models.ForeignKey( Books,on_delete=models.CASCADE,verbose_name='图书名称') # auto_created=True,当表中数据有改动时会自定更新时间 s_date = models.DateTimeField(auto_created=True,verbose_name='借书时间') name = models.CharField(max_length=32,verbose_name='借书人姓名') back_date = models.DateTimeField(auto_created=True,verbose_name='归还时间') back_status = models.BooleanField(default=False, verbose_name='归还状态') class Meta: db_table = 'SYS_Record' verbose_name = '借书记录表' def __str__(self): return self.name
2、视图代码:
class RecordView(View): """ 借还书接口 """ def post(self,request): # 登录权限校验 if not request.user.is_authenticated: return JsonResponse({'code':10001,'msg':'没有权限'}) params = request.POST if len(request.POST) > 0 else eval(request.body) id = params.get('id') book = params.get('book') name = params.get('name') # 1、校验参数不能为空 if not (id and name and book): return JsonResponse({'code':40001,'msg':'图书编号或图书名称或借书人不能为空','info':params}) # 2、检查图书是否存在 try: book_info =Books.objects.get(id=id) except Exception as e: return JsonResponse({'code':40002,'msg':'图书不存在'}) if book_info.record_status: return JsonResponse({'code':40003,'msg':'只能出借状态为未出借的图书'}) else: book_info.record_status = 'True' book_info.save() Record.objects.create(id=id, book=book, name=name) return JsonResponse({'code': 40004, 'msg': '借书成功'})
错误:
求解答,