Skip to content

Instantly share code, notes, and snippets.

@daimon99
daimon99 / admin.py
Last active April 30, 2022 23:06
Django admin different changeform in add / change。Django Admin新增与修改页面使用不同的Form。
@admin.register(m.Holiday)
class HolidayAdmin(admin.ModelAdmin):
list_display = ('id', 'day', 'category')
search_fields = ('day',)
list_filter = ('category',)
date_hierarchy = 'day'
def get_form(self, request, obj=None, change=False, **kwargs):
if not change:
@daimon99
daimon99 / admin.py
Last active January 21, 2020 00:56
Casade select pure frontend in django admin。纯前端实现Django Admin级联选择
class BudgetInline(admin.TabularInline):
class Media:
js = (JQUERY_MIN_JS, )
model = m.Budget
exclude = ['department', ]
readonly_fields = ['available_amount', ]
form = forms.BudgetAdminForm
@admin.register(m.Budget)
@daimon99
daimon99 / admin.py
Last active January 21, 2020 01:21
Money format style in django admin。金额样式,两位小数,纯前端实现(django admin)
@admin.register(m.Project)
class ProjectAdmin(admin.ModelAdmin):
formfield_overrides = {
models.DecimalField: {'widget': widgets.DecimalInput},
}
@daimon99
daimon99 / resources.py
Last active February 28, 2020 13:44
Custom django import export resource class, using verbose_name as export file header, and support clean field in the resource class。自定义的 django import export resource 类,支持用 verbose_name 作为导出/导入文件的列名,并支持在 Resouce 类中直接 clean 字段值
# coding: utf-8
from collections import OrderedDict
from copy import deepcopy
from django.core.exceptions import ValidationError
from django.utils.encoding import force_str
from import_export import resources, widgets
from . import models as m
@daimon99
daimon99 / print_code_to_docx.py
Created March 18, 2020 09:24
软件著作权代码打印到 word docx 文件
# coding: utf-8
# pip install python-docx
def get_all_python_files(directory):
import os, pathlib
all_python_files = list(pathlib.Path(os.path.expanduser(directory)).glob('./**/[!config]*/*.py'))
all_python_files.extend(list(pathlib.Path(os.path.expanduser(directory)).glob('*.py')))
return all_python_files
def is_not_excluded_text(line):
@daimon99
daimon99 / admin.py
Last active April 1, 2020 09:27
Django admin action with intermedia page.Django admin 的 action 使用中间页
@admin.register(WBS)
class WBSAdmin(admin.ModelAdmin):
list_display = (
'id',
'code',
'name',
'parent',
'pv',
'ev',
'created',
@daimon99
daimon99 / admin.py
Created April 15, 2020 03:53
Django admin 新建记录页面表单初始值 / Django admin add form initial value
def get_changeform_initial_data(self, request):
init = super().get_changeform_initial_data(request)
init['company'] = get_user_last_company(request.user)
return init
@daimon99
daimon99 / admin.py
Created April 15, 2020 04:16
Django admin 的新增和修改页面使用不同的字段 / Different fields for add and change pages in django admin
def get_fields(self, request, obj=None):
if not obj:
# 新增页面的字段
fields = ['name', 'company', 'project']
else:
# 修改页面的字段
fields = super().get_fields(request, obj)
return fields
@daimon99
daimon99 / index.html
Created April 18, 2020 04:25
javascript获取url中的get参数 / javascript get the get param in the url.
function findGetParameter(parameterName) {
var result = null,
tmp = [];
location.search
.substr(1)
.split("&")
.forEach(function (item) {
tmp = item.split("=");
if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
});
@daimon99
daimon99 / any.py
Created April 21, 2020 06:40
进入 ipython 调试控制台 / Debug using ipython embed
from IPython import embed;embed()