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
6b03179e
Kaydet (Commit)
6b03179e
authored
Ock 29, 2013
tarafından
Simon Charette
Kaydeden (comit)
Aymeric Augustin
Şub 24, 2013
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #19688 -- Allow model subclassing with a custom metaclass using six.with_metaclass
üst
e369dc28
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
52 additions
and
2 deletions
+52
-2
base.py
django/db/models/base.py
+11
-2
__init__.py
tests/modeltests/base/__init__.py
+0
-0
models.py
tests/modeltests/base/models.py
+5
-0
tests.py
tests/modeltests/base/tests.py
+36
-0
No files found.
django/db/models/base.py
Dosyayı görüntüle @
6b03179e
...
...
@@ -58,12 +58,21 @@ class ModelBase(type):
"""
def
__new__
(
cls
,
name
,
bases
,
attrs
):
super_new
=
super
(
ModelBase
,
cls
)
.
__new__
# six.with_metaclass() inserts an extra class called 'NewBase' in the
# inheritance tree: Model -> NewBase -> object. Ignore this class.
# inheritance tree: Model -> NewBase -> object. But the initialization
# should be executed only once for a given model class.
# attrs will never be empty for classes declared in the standard way
# (ie. with the `class` keyword). This is quite robust.
if
name
==
'NewBase'
and
attrs
==
{}:
return
super_new
(
cls
,
name
,
bases
,
attrs
)
# Also ensure initialization is only performed for subclasses of Model
# (excluding Model class itself).
parents
=
[
b
for
b
in
bases
if
isinstance
(
b
,
ModelBase
)
and
not
(
b
.
__name__
==
'NewBase'
and
b
.
__mro__
==
(
b
,
object
))]
if
not
parents
:
# If this isn't a subclass of Model, don't do anything special.
return
super_new
(
cls
,
name
,
bases
,
attrs
)
# Create the class.
...
...
tests/modeltests/base/__init__.py
0 → 100644
Dosyayı görüntüle @
6b03179e
tests/modeltests/base/models.py
0 → 100644
Dosyayı görüntüle @
6b03179e
from
django.db.models.base
import
ModelBase
class
CustomBaseModel
(
ModelBase
):
pass
tests/modeltests/base/tests.py
0 → 100644
Dosyayı görüntüle @
6b03179e
from
__future__
import
unicode_literals
from
django.db
import
models
from
django.test.testcases
import
SimpleTestCase
from
django.utils
import
six
from
django.utils.unittest
import
skipIf
from
.models
import
CustomBaseModel
class
CustomBaseTest
(
SimpleTestCase
):
@skipIf
(
six
.
PY3
,
'test metaclass definition under Python 2'
)
def
test_py2_custom_base
(
self
):
"""
Make sure models.Model can be subclassed with a valid custom base
using __metaclass__
"""
try
:
class
MyModel
(
models
.
Model
):
__metaclass__
=
CustomBaseModel
except
Exception
:
self
.
fail
(
"models.Model couldn't be subclassed with a valid "
"custom base using __metaclass__."
)
def
test_six_custom_base
(
self
):
"""
Make sure models.Model can be subclassed with a valid custom base
using `six.with_metaclass`.
"""
try
:
class
MyModel
(
six
.
with_metaclass
(
CustomBaseModel
,
models
.
Model
)):
pass
except
Exception
:
self
.
fail
(
"models.Model couldn't be subclassed with a valid "
"custom base using `six.with_metaclass`."
)
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