django admin添加数据自动记录user到表中的实现方法
1.需求:在后台添加一条数据的同时要把添加者记录到表中。
2.models.py
class Setting(models.Model): ... user = models.CharField(max_length=50, blank=True, help_text=u"添加者") ...
3.admin.py
@register(Setting) class SettingAdmin(admin.ModelAdmin): list_display = ("pk", "user", "other_field") def save_model(self, request, obj, form, change): obj.user = request.user obj.save() readonly_fields = ("user", )
相关推荐
taiyanghua 2020-07-29
cuiwenjie 2020-06-16
我心似明月 2020-11-09
chenyingSunny 2020-09-04
RisenWang 2020-07-19
hzyuhz 2020-06-25
hoooooolyhu 2020-06-11
就是那个胖子 2020-06-11
Nicolase 2020-06-10
Burgesszheng 2020-05-30
时光如瑾雨微凉 2020-05-26
Dreamya 2020-05-17
萌亖 2020-05-12
苦咖啡flask 2020-05-09
明月清风精进不止 2020-05-07
tuxlcsdn 2020-05-05
小周 2020-04-30
知识积累 2020-04-27