Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
03b69477
Kaydet (Commit)
03b69477
authored
Haz 05, 2015
tarafından
Claude Paroz
Kaydeden (comit)
Tim Graham
Mar 29, 2016
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #24932 -- Added Cast database function.
Thanks Ian Foote for the initial patch.
üst
53361589
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
77 additions
and
0 deletions
+77
-0
functions.py
django/db/models/functions.py
+33
-0
database-functions.txt
docs/ref/models/database-functions.txt
+18
-0
1.10.txt
docs/releases/1.10.txt
+2
-0
test_cast.py
tests/db_functions/test_cast.py
+24
-0
No files found.
django/db/models/functions.py
Dosyayı görüntüle @
03b69477
...
@@ -4,6 +4,39 @@ Classes that represent database functions.
...
@@ -4,6 +4,39 @@ Classes that represent database functions.
from
django.db.models
import
Func
,
Transform
,
Value
,
fields
from
django.db.models
import
Func
,
Transform
,
Value
,
fields
class
Cast
(
Func
):
"""
Coerce an expression to a new field type.
"""
function
=
'CAST'
template
=
'
%(function)
s(
%(expressions)
s AS
%(db_type)
s)'
mysql_types
=
{
fields
.
CharField
:
'char'
,
fields
.
IntegerField
:
'signed integer'
,
fields
.
FloatField
:
'signed'
,
}
def
__init__
(
self
,
expression
,
output_field
):
super
(
Cast
,
self
)
.
__init__
(
expression
,
output_field
=
output_field
)
def
as_sql
(
self
,
compiler
,
connection
,
**
extra_context
):
if
'db_type'
not
in
extra_context
:
extra_context
[
'db_type'
]
=
self
.
_output_field
.
db_type
(
connection
)
return
super
(
Cast
,
self
)
.
as_sql
(
compiler
,
connection
,
**
extra_context
)
def
as_mysql
(
self
,
compiler
,
connection
):
extra_context
=
{}
output_field_class
=
type
(
self
.
_output_field
)
if
output_field_class
in
self
.
mysql_types
:
extra_context
[
'db_type'
]
=
self
.
mysql_types
[
output_field_class
]
return
self
.
as_sql
(
compiler
,
connection
,
**
extra_context
)
def
as_postgresql
(
self
,
compiler
,
connection
):
# CAST would be valid too, but the :: shortcut syntax is more readable.
return
self
.
as_sql
(
compiler
,
connection
,
template
=
'
%(expressions)
s::
%(db_type)
s'
)
class
Coalesce
(
Func
):
class
Coalesce
(
Func
):
"""
"""
Chooses, from left to right, the first non-null expression and returns it.
Chooses, from left to right, the first non-null expression and returns it.
...
...
docs/ref/models/database-functions.txt
Dosyayı görüntüle @
03b69477
...
@@ -23,6 +23,24 @@ We don't usually recommend allowing ``null=True`` for ``CharField`` since this
...
@@ -23,6 +23,24 @@ We don't usually recommend allowing ``null=True`` for ``CharField`` since this
allows the field to have two "empty values", but it's important for the
allows the field to have two "empty values", but it's important for the
``Coalesce`` example below.
``Coalesce`` example below.
``Cast``
========
.. class:: Cast(expression, output_field)
.. versionadded:: 1.10
Forces the result type of ``expression`` to be the one from ``output_field``.
Usage example::
>>> from django.db.models import FloatField
>>> from django.db.models.functions import Cast
>>> Value.objects.create(integer=4)
>>> value = Value.objects.annotate(as_float=Cast('integer', FloatField)).get()
>>> print(value.as_float)
4.0
``Coalesce``
``Coalesce``
============
============
...
...
docs/releases/1.10.txt
Dosyayı görüntüle @
03b69477
...
@@ -349,6 +349,8 @@ Models
...
@@ -349,6 +349,8 @@ Models
* :meth:`QuerySet.bulk_create() <django.db.models.query.QuerySet.bulk_create>`
* :meth:`QuerySet.bulk_create() <django.db.models.query.QuerySet.bulk_create>`
sets the primary key on objects when using PostgreSQL.
sets the primary key on objects when using PostgreSQL.
* Added the :class:`~django.db.models.functions.Cast` database function.
Requests and Responses
Requests and Responses
~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~
...
...
tests/db_functions/test_cast.py
0 → 100644
Dosyayı görüntüle @
03b69477
from
django.db
import
models
from
django.db.models.expressions
import
Value
from
django.db.models.functions
import
Cast
from
django.test
import
TestCase
from
.models
import
Author
class
CastTests
(
TestCase
):
@classmethod
def
setUpTestData
(
self
):
Author
.
objects
.
create
(
name
=
'Bob'
,
age
=
1
)
def
test_cast_from_value
(
self
):
numbers
=
Author
.
objects
.
annotate
(
cast_integer
=
Cast
(
Value
(
'0'
),
models
.
IntegerField
()))
self
.
assertEqual
(
numbers
.
get
()
.
cast_integer
,
0
)
def
test_cast_from_field
(
self
):
numbers
=
Author
.
objects
.
annotate
(
cast_string
=
Cast
(
'age'
,
models
.
CharField
(
max_length
=
255
)),)
self
.
assertEqual
(
numbers
.
get
()
.
cast_string
,
'1'
)
def
test_cast_from_python
(
self
):
numbers
=
Author
.
objects
.
annotate
(
cast_float
=
Cast
(
0
,
models
.
FloatField
()))
self
.
assertEqual
(
numbers
.
get
()
.
cast_float
,
0.0
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment