本文整理汇总了Python中django.db.models.Func方法的典型用法代码示例。如果您正苦于以下问题:Python models.Func方法的具体用法?Python models.Func怎么用?Python models.Func使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models
的用法示例。
在下文中一共展示了models.Func方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: clean_domain_name
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import Func [as 别名]
def clean_domain_name(field_name):
"""
Returns a Django query expression that replaces all underscores with a
hyphen, removes leading and trailing hyphens, and converts the field to
lower case.
"""
remove_underscores = Func(
F(field_name),
Value('_'),
Value('-'),
function='replace'
)
trim_hyphens = Func(
remove_underscores,
Value('-'),
function='btrim',
)
remove_trailing_hyphens = Func(
trim_hyphens,
Value(r'[-]+\.'),
Value('.'),
Value('g'),
function='regexp_replace'
)
return Lower(remove_trailing_hyphens)
示例2: change_metakey_case
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import Func [as 别名]
def change_metakey_case(self, metakey, operation, modifier=None):
available_operations = {'lowercase': lowercase,
'uppercase': uppercase,
'capitalize': capitalize}
edit = available_operations.get(operation, None)
if edit:
if modifier:
new_metakey = metakey.replace(modifier, edit(modifier))
else:
new_metakey = edit(metakey)
# Copy old metadata into an hstore container with new key value
TEMP_HSTORE = Func(Value(new_metakey), GET_VALUE('metadata') + Value(metakey), function='hstore')
# Delete old key entry from original hstore
META_HSTORE = Func(F('metadata'), Value(metakey), function='delete')
# Combine the two hstores using internal 'hs_concat' function
CONCAT_HSTORE = Func(TEMP_HSTORE, META_HSTORE, function='hs_concat')
self.filter(metadata__has_key=metakey).update(metadata=CONCAT_HSTORE)
示例3: set_metadata
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import Func [as 别名]
def set_metadata(self, metakey, metavalue):
if check_lexical_convention(metakey) and \
(metavalue.isdigit() or metavalue in ('true', 'false') or \
(metavalue.startswith('\"') and metavalue.endswith('\"'))):
# Copy old metadata into an hstore container with new key value
TEMP_HSTORE = Func(Value(metakey), Value(metavalue), function='hstore')
# Delete old key entry from original hstore
META_HSTORE = Func(F('metadata'), Value(metakey), function='delete')
# Combine the two hstores using internal 'hs_concat' function
CONCAT_HSTORE = Func(TEMP_HSTORE, META_HSTORE, function='hs_concat')
self.update(metadata=CONCAT_HSTORE)
示例4: infer_practice_boundaries
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import Func [as 别名]
def infer_practice_boundaries():
practices = Practice.objects.filter(location__isnull=False, setting=4).exclude(
status_code__in=(
Practice.STATUS_RETIRED,
Practice.STATUS_DORMANT,
Practice.STATUS_CLOSED,
)
)
partition = practices.aggregate(
voronoi=Func(Collect("location"), function="ST_VoronoiPolygons")
)["voronoi"]
national_boundary = get_national_boundary()
practice_regions = get_practice_code_to_region_map(partition, national_boundary)
with transaction.atomic():
for practice in practices:
practice.boundary = practice_regions[practice.code]
practice.save(update_fields=["boundary"])
示例5: as_oracle
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import Func [as 别名]
def as_oracle(self, compiler, connection):
# we can't mix TextField (NCLOB) and CharField (NVARCHAR), so convert
# all fields to NCLOB when we expect NCLOB
if self.output_field.get_internal_type() == 'TextField':
class ToNCLOB(Func):
function = 'TO_NCLOB'
expressions = [
ToNCLOB(expression) for expression in self.get_source_expressions()]
clone = self.copy()
clone.set_source_expressions(expressions)
return super(Coalesce, clone).as_sql(compiler, connection)
return self.as_sql(compiler, connection)
示例6: populate_date_range
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import Func [as 别名]
def populate_date_range(apps, schema_editor):
Activity = apps.get_model('activities', 'Activity')
# initializes the date_range field with a range of (date, date + 30 minutes)
Activity.objects.all().update(
date_range=Func(F('date'), F('date') + timedelta(minutes=30), function='tstzrange')
)
示例7: __init__
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import Func [as 别名]
def __init__(self, lhs, rhs):
super().__init__(Func(Func(lhs, function='F_UNACCENT'), function='UPPER'), Func(Func(rhs, function='F_UNACCENT'), function='UPPER'))
示例8: get_prep_value
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import Func [as 别名]
def get_prep_value(self, value):
# convert the Molecule instance to the value used by the
# db driver
if isinstance(value, str):
value = self.text_to_mol(value)
if isinstance(value, Chem.Mol):
value = memoryview(value.ToBinary())
if value is None:
return None
return Func(value, function='mol_from_pkl')
示例9: as_oracle
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import Func [as 别名]
def as_oracle(self, compiler, connection):
# Oracle prohibits mixing TextField (NCLOB) and CharField (NVARCHAR2),
# so convert all fields to NCLOB when that type is expected.
if self.output_field.get_internal_type() == 'TextField':
class ToNCLOB(Func):
function = 'TO_NCLOB'
expressions = [
ToNCLOB(expression) for expression in self.get_source_expressions()
]
clone = self.copy()
clone.set_source_expressions(expressions)
return super(Coalesce, clone).as_sql(compiler, connection)
return self.as_sql(compiler, connection)
示例10: current_event_types
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import Func [as 别名]
def current_event_types(self):
qs = (ProbeSource.objects.annotate(event_type=Func(F("event_types"), function="unnest"))
.values("event_type").distinct().order_by())
return sorted(((rd["event_type"], event_types[rd["event_type"]].get_event_type_display())
for rd in qs),
key=lambda t: t[1])
示例11: test_Func_expresion
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import Func [as 别名]
def test_Func_expresion(self):
# initialize
ini_values = 'aA', 'BB', '', 'cc', '12'
people = Person.objects.order_by('pk').all()
for value, person in zip(ini_values, people):
person.name = value
person.text = value*2
person.save()
# set
people = Person.objects.order_by('pk').all()
for person in people:
person.name = Func(F('name'), function='UPPER')
person.text = Func(F('text'), function='LOWER')
# update
Person.objects.bulk_update(people)
# check
people = Person.objects.order_by('pk').all()
expected_values = 'AA', 'BB', '', 'CC', '12'
for expected_value, person in zip(expected_values, people):
saved_value = person.name
self.assertEqual(saved_value, expected_value)
expected_values = 'aaaa', 'bbbb', '', 'cccc', '1212'
for expected_value, person in zip(expected_values, people):
saved_value = person.text
self.assertEqual(saved_value, expected_value)
示例12: test_already_evaluated_queryset
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import Func [as 别名]
def test_already_evaluated_queryset(self):
"""
Queries:
- update objects
(objects are already retrieved, because of the previous loop)
"""
people = Person.objects.all()
for person in people:
person.age += 2
person.name = Func(F('name'), function='UPPER')
person.text = 'doc'
person.height -= Decimal(0.5)
self.assertNumQueries(1, Person.objects.bulk_update, people)
示例13: move_collection_receiver
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import Func [as 别名]
def move_collection_receiver(sender, **kwargs):
"""
A post_save receiver that is triggered when Collection model objects are changed.
"""
if kwargs["created"]:
return
collection = kwargs["instance"]
try:
prev_collection = collection.history.all()[1]
except IndexError:
return
if collection.collection_set != prev_collection.collection_set:
src_collection_path = get_collection_path(prev_collection)
dest_collection_path = get_collection_path(collection)
if not os.path.exists(src_collection_path):
log.warn("Not moving collection directory since %s does not exist", src_collection_path)
return
if os.path.exists(dest_collection_path):
log.error("Cannot move collection directory %s since %s already exists")
return
log.info("Moving %s to %s", src_collection_path, dest_collection_path)
shutil.move(src_collection_path, dest_collection_path)
# Update WARC model objects
Warc.objects.filter(harvest__collection=collection).update(
path=Func(
F('path'),
Value(src_collection_path), Value(dest_collection_path),
function='replace',
)
)
示例14: rename_metakey
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import Func [as 别名]
def rename_metakey(self, old_metakey, new_metakey):
if check_lexical_convention(new_metakey):
# Copy old metadata into an hstore container with new key value
TEMP_HSTORE = Func(Value(new_metakey), GET_VALUE('metadata') + Value(old_metakey), function='hstore')
# Delete old key entry from original hstore
META_HSTORE = Func(F('metadata'), Value(old_metakey), function='delete')
# Combine the two hstores using internal 'hs_concat' function
CONCAT_HSTORE = Func(TEMP_HSTORE, META_HSTORE, function='hs_concat')
self.filter(metadata__has_key=old_metakey).update(metadata=CONCAT_HSTORE)
示例15: _divide
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import Func [as 别名]
def _divide(numerator_field, denominator_field):
"""
SQL division function which handles NULLs and divide-by-zero gracefully
"""
numerator = Coalesce(numerator_field, Value(0.0))
denominator = Func(denominator_field, Value(0.0), function="NULLIF")
return numerator / denominator