💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
>[success] # Fomrs ~~~ 1. 自定义admin input输入框 2. 做字段的验证 3. 做表和表之间的关联 ~~~ >[success] # 做forms 表单验证 ~~~ 1.在创建的app 文件下创建 forms 表单 2.在admin.py 下引入 forms 表单的验证 ~~~ >[danger] ##### 创建文件adminforms.py ~~~ 1.创建的名字随便起 ~~~ ~~~ from django import forms class PostAdminForm(forms.ModelForm): status = forms.BooleanField(label="是否删除", required=True) desc = forms.CharField(widget=forms.Textarea, label="摘要", required=False) # 处理字段 def clean_status(self): if self.cleaned_data['status']: return 1 else: return 3 ~~~ >[danger] ##### 在admin.py 中引入 ~~~ from .adminforms import PostAdminForm @admin.register(Post, site=custom_site) class PostAdmin(admin.ModelAdmin): form = PostAdminForm ~~~ >[success] # 两个表关联同时编辑外键和数据inline ![](https://box.kancloud.cn/3831c24eaff38390b4c3c45e33c0d81c_1324x582.png) ~~~ from django.contrib import admin class PostInline(admin.TabularInline): # StackedInline 样式不同 fields = ('title', 'desc') extra = 1 # 控制额外多几个 model = Post class CategoryAdmin(admin.ModelAdmin): inlines = [PostInline, ] ~~~ >[danger] ##### 重写save_model ~~~ def save_model(self, request, obj, form, change): obj.owner = request.user super(PostAdmin, self).save_model(request, obj, form, change) ~~~