本文整理汇总了Python中django.forms.models.model_to_dict方法的典型用法代码示例。如果您正苦于以下问题:Python models.model_to_dict方法的具体用法?Python models.model_to_dict怎么用?Python models.model_to_dict使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.forms.models
的用法示例。
在下文中一共展示了models.model_to_dict方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: movedown
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import model_to_dict [as 别名]
def movedown(request, queryset):
action = sys._getframe().f_code.co_name
action_name = "下架"
if request.POST.get('post'):
for obj in queryset:
o = copy.deepcopy(obj)
obj.actived = False
obj.status = 'offline'
obj.operator = request.user
obj.units.all().update(actived=True, operator=obj.operator)
obj.pdus.all().update(actived=True, operator=obj.operator)
obj.save()
diffs = diff_dict(model_to_dict(o), model_to_dict(obj))
log_action(
user_id=request.user.pk,
content_type_id=get_content_type_for_model(obj, True).pk,
object_id=obj.pk, action_flag=action_name,
message=json.dumps(list(diffs.keys())),
content=json.dumps(diffs)
)
return None
context = construct_context(request, queryset, action, action_name)
return TemplateResponse(request, 'base/base_confirmation.html', context)
示例2: actived
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import model_to_dict [as 别名]
def actived(request, queryset):
action = sys._getframe().f_code.co_name
action_name = "停用"
if request.POST.get('post'):
for obj in queryset:
o = copy.deepcopy(obj)
obj.actived = False
obj.save()
diffs = diff_dict(model_to_dict(o), model_to_dict(obj))
log_action(
user_id=request.user.pk,
content_type_id=get_content_type_for_model(obj, True).pk,
object_id=obj.pk,
action_flag="停用",
message=json.dumps(list(diffs.keys())),
content=json.dumps(diffs)
)
return None
context = construct_context(request, queryset, action, action_name)
return TemplateResponse(request, 'base/base_confirmation.html', context)
示例3: reclaim
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import model_to_dict [as 别名]
def reclaim(request, queryset):
action = sys._getframe().f_code.co_name
action_name = "回收"
if request.POST.get('post'):
for obj in queryset:
o = copy.deepcopy(obj)
obj.actived = False
obj.save()
diffs = diff_dict(model_to_dict(o), model_to_dict(obj))
log_action(
user_id=request.user.pk,
content_type_id=get_content_type_for_model(obj, True).pk,
object_id=obj.pk,
action_flag=action_name,
message=json.dumps(list(diffs.keys())),
content=json.dumps(diffs)
)
return None
context = construct_context(request, queryset, action, action_name)
return TemplateResponse(request, 'base/base_confirmation.html', context)
示例4: cancel_reclaim
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import model_to_dict [as 别名]
def cancel_reclaim(request, queryset):
action = sys._getframe().f_code.co_name
action_name = "取消回收"
if request.POST.get('post'):
for obj in queryset:
o = copy.deepcopy(obj)
obj.actived = True
obj.save()
diffs = diff_dict(model_to_dict(o), model_to_dict(obj))
log_action(
user_id=request.user.pk,
content_type_id=get_content_type_for_model(obj, True).pk,
object_id=obj.pk,
action_flag=action_name,
message=json.dumps(list(diffs.keys())),
content=json.dumps(diffs)
)
return None
context = construct_context(request, queryset, action, action_name)
return TemplateResponse(request, 'base/base_confirmation.html', context)
示例5: reoutbound
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import model_to_dict [as 别名]
def reoutbound(request, queryset):
action = sys._getframe().f_code.co_name
action_name = "取消出库"
queryset = queryset.filter(actived=False)
if not queryset.exists():
return "查无结果"
if request.POST.get('post'):
for obj in queryset:
o = copy.deepcopy(obj)
obj.actived = True
obj.save()
diffs = diff_dict(model_to_dict(o), model_to_dict(obj))
log_action(
user_id=request.user.pk,
content_type_id=get_content_type_for_model(obj, True).pk,
object_id=obj.pk,
action_flag=action_name,
message=json.dumps(list(diffs.keys())),
content=json.dumps(diffs)
)
return None
context = construct_context(request, queryset, action, action_name)
return TemplateResponse(request, 'base/base_confirmation.html', context)
示例6: as_dict
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import model_to_dict [as 别名]
def as_dict(self):
"""
Returns dictionary representation of attributes, excluding all attributes not associated with the ``type`` of
this ``Target``.
:returns: Dictionary of key/value pairs representing target attributes
:rtype: dict
"""
if self.type == self.SIDEREAL:
fields_for_type = SIDEREAL_FIELDS
elif self.type == self.NON_SIDEREAL:
fields_for_type = NON_SIDEREAL_FIELDS
else:
fields_for_type = GLOBAL_TARGET_FIELDS
return model_to_dict(self, fields=fields_for_type)
示例7: form_with_data
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import model_to_dict [as 别名]
def form_with_data(segment, *rules):
model_fields = ['type', 'status', 'count', 'name', 'match_any', 'randomisation_percent']
class TestSegmentAdminForm(SegmentAdminForm):
class Meta:
model = Segment
fields = model_fields
data = model_to_dict(segment, model_fields)
for formset in TestSegmentAdminForm().formsets.values():
rule_data = {}
count = 0
for rule in rules:
if isinstance(rule, formset.model):
rule_data = model_to_dict(rule)
for key, value in rule_data.items():
data['{}-{}-{}'.format(formset.prefix, count, key)] = value
count += 1
data['{}-INITIAL_FORMS'.format(formset.prefix)] = 0
data['{}-TOTAL_FORMS'.format(formset.prefix)] = count
return TestSegmentAdminForm(data)
示例8: serialize_for_messagebus
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import model_to_dict [as 别名]
def serialize_for_messagebus(self):
""" Serialize the object for posting to messagebus.
The API serializes ForeignKey fields by naming them <name>_id
whereas model_to_dict leaves them with the original name. Modify
the results of model_to_dict to provide the same fieldnames.
"""
field_types = {}
for f in self._meta.fields:
field_types[f.name] = f.get_internal_type()
fields = model_to_dict(self)
for k in fields.keys():
if field_types.get(k, None) == "ForeignKey":
new_key_name = "%s_id" % k
if (k in fields) and (new_key_name not in fields):
fields[new_key_name] = fields[k]
del fields[k]
return fields
示例9: case_query
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import model_to_dict [as 别名]
def case_query(request):
if request.method == "POST":
case_id = request.POST.get('id')
cases = TestCaseInfo.objects.filter(id=case_id)
moduleid_to_projectid = {}
moduleid_to_modulename = {}
if len(cases) == 0:
return JsonResponse(get_ajax_msg(0, 0, '没有这条数据', {}))
if cases is not None and len(cases) > 0:
for case in cases:
# 所属模块id对应模块名称
module_id = case.belong_module_id
module_name = case.belong_module.module_name
moduleid_to_modulename[str(module_id)] = module_name
# 所属模块id对应项目id
project_id = case.belong_module.belong_project_id
moduleid_to_projectid[str(module_id)] = project_id
cases = filter_cases_for_user(request.user, cases, AUTH_VIEW)
data = dataToJson([model_to_dict(i) for i in cases])
data = json.loads(data)
return JsonResponse(get_ajax_msg(1, 1, '获取用例信息成功', {'cases': data, 'proInfo': moduleid_to_projectid,
'moduleInfo': moduleid_to_modulename}))
示例10: env_search
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import model_to_dict [as 别名]
def env_search(request):
if request.method == "POST":
case_id = request.POST.get('case_id')
belong_project = 0
if case_id is not None:
# 通过测试用例id查询所能用的环境
cases = TestCaseInfo.objects.filter(id=case_id)
if len(cases) == 0:
return JsonResponse(get_ajax_msg(0, 0, '没有这条用例!', {}))
else:
case = cases[0]
belong_project = case.belong_module.belong_project_id
else:
belong_project = request.POST.get('project_id')
if belong_project is None:
return JsonResponse(get_ajax_msg(0, 0, '数据无效', {}))
envs = EnvInfo.objects.filter(belong_project_id=belong_project)
data = dataToJson([model_to_dict(i) for i in envs])
return JsonResponse(get_ajax_msg(1, 1, '获取用环境息成功', {'envs': data}))
示例11: module_list
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import model_to_dict [as 别名]
def module_list(request):
if request.method == 'GET':
return render(request, 'api/module_list.html')
elif request.method == 'POST':
index = int(request.POST.get('index'))
project_name_dic = {}
# 根据用户权限筛选模块
objects = filter_modules_for_user(request.user, ModuleInfo.objects.order_by('-id'), AUTH_VIEW)
modules = pagination_for_objects(objects, index)
if modules is not None and len(modules) > 0:
for module in modules:
project_id = module.belong_project_id
project_name = module.belong_project.project_name
project_name_dic[str(project_id)] = project_name
count = len(objects)
data = dataToJson([model_to_dict(i) for i in modules])
return JsonResponse(get_ajax_msg(1, 1, '获取模块列表成功', {'modules': data, 'count': count, 'currPage': index,
'proInfo': project_name_dic}))
示例12: project_search
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import model_to_dict [as 别名]
def project_search(request):
if request.method == "POST":
project_name = request.POST.get('project_name')
person_name = request.POST.get('person_name')
index = int(request.POST.get('index'))
if len(project_name) == 0 and len(person_name) == 0:
return JsonResponse(get_ajax_msg(0, 0, '搜索条件无效'))
else:
projects = ProjectInfo.objects.all()
if len(project_name) > 0:
projects = projects.filter(project_name__contains=project_name)
if len(person_name) > 0:
projects = projects.filter(responsible_name__contains=person_name)
if projects is None:
return JsonResponse(get_ajax_msg(0, 0, '查询出错'))
objects = get_objects_for_user(request.user, AUTH_VIEW, projects) # 根据用户权限筛选项目对象
projects = pagination_for_objects(objects, index)
count = objects.count()
data = dataToJson([model_to_dict(i) for i in projects])
return JsonResponse(get_ajax_msg(1, 1, '搜索成功', {'projects': data, 'count': count, 'currPage': index}))
示例13: build_realmemoji
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import model_to_dict [as 别名]
def build_realmemoji(custom_emoji_list: ZerverFieldsT,
realm_id: int) -> Tuple[List[ZerverFieldsT],
ZerverFieldsT]:
zerver_realmemoji = []
emoji_url_map = {}
emoji_id = 0
for emoji_name, url in custom_emoji_list.items():
if 'emoji.slack-edge.com' in url:
# Some of the emojis we get from the api have invalid links
# this is to prevent errors related to them
realmemoji = RealmEmoji(
name=emoji_name,
id=emoji_id,
file_name=os.path.basename(url),
deactivated=False)
realmemoji_dict = model_to_dict(realmemoji, exclude=['realm', 'author'])
realmemoji_dict['author'] = None
realmemoji_dict['realm'] = realm_id
emoji_url_map[emoji_name] = url
zerver_realmemoji.append(realmemoji_dict)
emoji_id += 1
return zerver_realmemoji, emoji_url_map
示例14: build_customprofilefields_values
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import model_to_dict [as 别名]
def build_customprofilefields_values(slack_custom_field_name_to_zulip_custom_field_id: ZerverFieldsT,
fields: ZerverFieldsT, user_id: int, custom_field_id: int,
custom_field_values: List[ZerverFieldsT]) -> int:
for field, value in fields.items():
if value['value'] == "":
continue
custom_field_value = CustomProfileFieldValue(
id=custom_field_id,
value=value['value'])
custom_field_value_dict = model_to_dict(custom_field_value,
exclude=['user_profile', 'field'])
custom_field_value_dict['user_profile'] = user_id
custom_field_value_dict['field'] = slack_custom_field_name_to_zulip_custom_field_id[field]
custom_field_values.append(custom_field_value_dict)
custom_field_id += 1
return custom_field_id
示例15: build_message
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import model_to_dict [as 别名]
def build_message(topic_name: str, date_sent: float, message_id: int, content: str,
rendered_content: Optional[str], user_id: int, recipient_id: int,
has_image: bool=False, has_link: bool=False,
has_attachment: bool=True) -> ZerverFieldsT:
zulip_message = Message(
rendered_content_version=1, # this is Zulip specific
date_sent=date_sent,
id=message_id,
content=content,
rendered_content=rendered_content,
has_image=has_image,
has_attachment=has_attachment,
has_link=has_link)
zulip_message.set_topic_name(topic_name)
zulip_message_dict = model_to_dict(zulip_message,
exclude=['recipient', 'sender', 'sending_client'])
zulip_message_dict['sender'] = user_id
zulip_message_dict['sending_client'] = 1
zulip_message_dict['recipient'] = recipient_id
return zulip_message_dict