本文整理汇总了Python中playhouse.shortcuts.model_to_dict方法的典型用法代码示例。如果您正苦于以下问题:Python shortcuts.model_to_dict方法的具体用法?Python shortcuts.model_to_dict怎么用?Python shortcuts.model_to_dict使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类playhouse.shortcuts
的用法示例。
在下文中一共展示了shortcuts.model_to_dict方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: update
# 需要导入模块: from playhouse import shortcuts [as 别名]
# 或者: from playhouse.shortcuts import model_to_dict [as 别名]
def update(self, fields_to_update=None):
if not fields_to_update:
fields_to_update = model_to_dict(self, recurse=False)
model_id = fields_to_update['id']
del fields_to_update['id']
model_class = self.__class__
query = super(PeeweeModel, self).update(**fields_to_update)
query = query.where(model_class.id == model_id)
query.execute()
for field, value in fields_to_update.items():
setattr(self, field, value)
return self
示例2: _to_dict
# 需要导入模块: from playhouse import shortcuts [as 别名]
# 或者: from playhouse.shortcuts import model_to_dict [as 别名]
def _to_dict(self):
data = {}
fields = self.val._meta.fields
for name, v in model_to_dict(self.val, recurse=False).items():
if isinstance(fields[name], peewee.ForeignKeyField):
name = name + '_id'
elif isinstance(fields[name], peewee.BlobField):
v = get_bytes_from_blob(v)
if self.selected != ALL_COLUMNS and (self.selected and (name not in self.selected)):
continue
data[name] = v
if self.available_columns != ALL_COLUMNS:
return dict_filter(data, self.available_columns)
return data
示例3: handle_session
# 需要导入模块: from playhouse import shortcuts [as 别名]
# 或者: from playhouse.shortcuts import model_to_dict [as 别名]
def handle_session():
uid = 0
paths = request.path.split('/')
if len(paths) > 1 and paths[1].isdigit():
uid = int(paths[1])
if 'user' in session and ((not uid) or (uid and session['user']['uid'] == uid)):
g.user = session['user']
elif uid:
user = FetchedUser.get_or_none(FetchedUser.uid == uid)
if not user:
abort(404, "no such user")
session['user'] = model_to_dict(user)
g.user = session['user']
else:
g.user = None
示例4: get_data
# 需要导入模块: from playhouse import shortcuts [as 别名]
# 或者: from playhouse.shortcuts import model_to_dict [as 别名]
def get_data(self):
if self.cache and self.advanced == self.cache_advanced:
return self.cache
self.fields, self.map_list, self.folder_info, self.folder_instance = \
await self.folder_manager.get_folder_code_contents(self.folder_code)
self.title = 'Folder: ' + self.folder_info['name']
karma = any(f['index'] == "karma" for f in self.fields)
length = any(f['index'] == "local_record" for f in self.fields)
items = []
for item in self.map_list:
dict_item = model_to_dict(item)
if length:
dict_item['local_record'] = times.format_time((item.local['first_record'].score if hasattr(item, 'local') and item.local['first_record'] else 0))
if karma and 'karma' in self.app.instance.apps.apps:
dict_item['karma'] = (await self.app.instance.apps.apps['karma'].get_map_karma(item))['map_karma']
items.append(dict_item)
self.cache = items
return self.cache
示例5: get_aircraft
# 需要导入模块: from playhouse import shortcuts [as 别名]
# 或者: from playhouse.shortcuts import model_to_dict [as 别名]
def get_aircraft(icao24):
icao24 = icao24.lower()
try:
obj = Plane.get(icao24 = icao24)
except Plane.DoesNotExist:
abort(404)
o = model_to_dict(obj)
# model_to_dict does not format dates
try:
o['added_on'] = str(obj.added_on.strftime('%Y-%m-%d %H:%M:%S'))
except AttributeError:
# String is not a date string
o['added_on'] = None
try:
o['updated_on'] = str(obj.updated_on.strftime('%Y-%m-%d %H:%M:%S'))
except AttributeError:
o['updated_on'] = None
return o
示例6: get_airport
# 需要导入模块: from playhouse import shortcuts [as 别名]
# 或者: from playhouse.shortcuts import model_to_dict [as 别名]
def get_airport(ica_or_iata):
ica_or_iata = ica_or_iata.upper()
try:
obj = Airport.get(icao = ica_or_iata)
except Airport.DoesNotExist:
try:
obj = Airport.get(iata = ica_or_iata)
except Airport.DoesNotExist:
abort(404)
o = model_to_dict(obj)
# model_to_dict does not format dates
o['lat'] = "%f" % obj.lat
o['lon'] = "%f" % obj.lon
o['alt'] = "%d" % obj.alt
o['added_on'] = str(obj.added_on.strftime('%Y-%m-%d %H:%M:%S'))
o['updated_on'] = str(obj.updated_on.strftime('%Y-%m-%d %H:%M:%S'))
return o
示例7: get_airline
# 需要导入模块: from playhouse import shortcuts [as 别名]
# 或者: from playhouse.shortcuts import model_to_dict [as 别名]
def get_airline(icao):
icao = icao.upper()
try:
obj = Airline.get(icao = icao)
except Airline.DoesNotExist:
abort(404)
o = model_to_dict(obj)
# model_to_dict does not format dates
try:
o['added_on'] = str(obj.added_on.strftime('%Y-%m-%d %H:%M:%S'))
except AttributeError:
# String is not a date string
o['added_on'] = None
try:
o['updated_on'] = str(obj.updated_on.strftime('%Y-%m-%d %H:%M:%S'))
except AttributeError:
o['updated_on'] = None
return o
示例8: get_route
# 需要导入模块: from playhouse import shortcuts [as 别名]
# 或者: from playhouse.shortcuts import model_to_dict [as 别名]
def get_route(flight):
flight = flight.upper()
try:
obj = Route.get(flight = flight)
except Route.DoesNotExist:
abort(404)
o = model_to_dict(obj)
# model_to_dict does not format dates
try:
o['added_on'] = str(obj.added_on.strftime('%Y-%m-%d %H:%M:%S'))
except AttributeError:
# String is not a date string
o['added_on'] = None
try:
o['updated_on'] = str(obj.updated_on.strftime('%Y-%m-%d %H:%M:%S'))
except AttributeError:
o['updated_on'] = None
return o
示例9: post
# 需要导入模块: from playhouse import shortcuts [as 别名]
# 或者: from playhouse.shortcuts import model_to_dict [as 别名]
def post(self, request):
valid_request = self.validate_request(request)
if valid_request is not True:
return valid_request
try:
result = self.model.create(**request.json)
return self.response_json(data=model_to_dict(result),
status_code=200,
message=self.config.response_messages.SuccessRowCreated.format(result.id)
)
except Exception as e:
log.error(traceback.print_exc())
return self.response_json(message=str(e),
status_code=500
)
示例10: report
# 需要导入模块: from playhouse import shortcuts [as 别名]
# 或者: from playhouse.shortcuts import model_to_dict [as 别名]
def report(self, machine: GordoMachine):
"""
Log a machine to Postgres where top level keys, 'name', 'dataset', 'model',
and 'metadata' mappings to BinaryJSON fields.
Parameters
----------
machine: gordo.machine.Machine
Returns
-------
None
"""
try:
with self.db.atomic():
logger.info(f"Inserting machine {machine.name} in sql") # type: ignore
# Ensure it's serializable using MachineEncoder
record = json.loads(json.dumps(machine.to_dict(), cls=MachineEncoder))
model = dict_to_model(Machine, record, ignore_unknown=True)
try:
Machine.get(Machine.name == machine.name)
except peewee.DoesNotExist:
model.save()
else:
query = Machine.update(**model_to_dict(model)).where(
Machine.name == machine.name
)
query.execute()
except Exception as exc:
raise PostgresReporterException(exc)
示例11: models_to_dicts
# 需要导入模块: from playhouse import shortcuts [as 别名]
# 或者: from playhouse.shortcuts import model_to_dict [as 别名]
def models_to_dicts(query_models):
dict_list = []
for query_model in query_models:
dict_list.append(model_to_dict(query_model))
return dict_list
示例12: search_project
# 需要导入模块: from playhouse import shortcuts [as 别名]
# 或者: from playhouse.shortcuts import model_to_dict [as 别名]
def search_project():
project_url = request.args.get('project_url', '')
if project_url:
try:
content = Content.select()\
.where(Content.project_url == project_url)\
.get()
return jsonify(payload=model_to_dict(content))
except DoesNotExist:
return jsonify(message='未找到')
else:
raise InvalidParams()
示例13: to_json
# 需要导入模块: from playhouse import shortcuts [as 别名]
# 或者: from playhouse.shortcuts import model_to_dict [as 别名]
def to_json(self):
return model_to_dict(self, recurse=False)
示例14: to_dict
# 需要导入模块: from playhouse import shortcuts [as 别名]
# 或者: from playhouse.shortcuts import model_to_dict [as 别名]
def to_dict(self):
return model_to_dict(self)
示例15: to_dict
# 需要导入模块: from playhouse import shortcuts [as 别名]
# 或者: from playhouse.shortcuts import model_to_dict [as 别名]
def to_dict(self, **kw):
return shortcuts.model_to_dict(self, **kw)