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
ec9ed074
Kaydet (Commit)
ec9ed074
authored
Eyl 20, 2016
tarafından
Michael Scott
Kaydeden (comit)
Tim Graham
Eki 29, 2016
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #27188 -- Allowed using unique=True with FileField.
Thanks Tim Graham for the initial patch.
üst
625cd5bc
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
23 additions
and
34 deletions
+23
-34
files.py
django/db/models/fields/files.py
+0
-14
checks.txt
docs/ref/checks.txt
+1
-0
fields.txt
docs/ref/models/fields.txt
+8
-4
1.11.txt
docs/releases/1.11.txt
+3
-0
test_ordinary_fields.py
tests/invalid_models_tests/test_ordinary_fields.py
+0
-15
models.py
tests/model_fields/models.py
+1
-1
test_filefield.py
tests/model_fields/test_filefield.py
+10
-0
No files found.
django/db/models/fields/files.py
Dosyayı görüntüle @
ec9ed074
...
...
@@ -230,7 +230,6 @@ class FileField(Field):
def
__init__
(
self
,
verbose_name
=
None
,
name
=
None
,
upload_to
=
''
,
storage
=
None
,
**
kwargs
):
self
.
_primary_key_set_explicitly
=
'primary_key'
in
kwargs
self
.
_unique_set_explicitly
=
'unique'
in
kwargs
self
.
storage
=
storage
or
default_storage
self
.
upload_to
=
upload_to
...
...
@@ -240,22 +239,9 @@ class FileField(Field):
def
check
(
self
,
**
kwargs
):
errors
=
super
(
FileField
,
self
)
.
check
(
**
kwargs
)
errors
.
extend
(
self
.
_check_unique
())
errors
.
extend
(
self
.
_check_primary_key
())
return
errors
def
_check_unique
(
self
):
if
self
.
_unique_set_explicitly
:
return
[
checks
.
Error
(
"'unique' is not a valid argument for a
%
s."
%
self
.
__class__
.
__name__
,
obj
=
self
,
id
=
'fields.E200'
,
)
]
else
:
return
[]
def
_check_primary_key
(
self
):
if
self
.
_primary_key_set_explicitly
:
return
[
...
...
docs/ref/checks.txt
Dosyayı görüntüle @
ec9ed074
...
...
@@ -183,6 +183,7 @@ File Fields
~~~~~~~~~~~
* **fields.E200**: ``unique`` is not a valid argument for a ``FileField``.
*This check is removed in Django 1.11*.
* **fields.E201**: ``primary_key`` is not a valid argument for a ``FileField``.
* **fields.E210**: Cannot use ``ImageField`` because Pillow is not installed.
...
...
docs/ref/models/fields.txt
Dosyayı görüntüle @
ec9ed074
...
...
@@ -306,12 +306,16 @@ you try to save a model with a duplicate value in a :attr:`~Field.unique`
field, a :exc:`django.db.IntegrityError` will be raised by the model's
:meth:`~django.db.models.Model.save` method.
This option is valid on all field types except :class:`ManyToManyField`
,
:class:`OneToOneField`
, and :class:`FileField`
.
This option is valid on all field types except :class:`ManyToManyField`
and
:class:`OneToOneField`.
Note that when ``unique`` is ``True``, you don't need to specify
:attr:`~Field.db_index`, because ``unique`` implies the creation of an index.
.. versionchanged:: 1.11
In older versions, ``unique=True`` can't be used on :class:`FileField`.
``unique_for_date``
-------------------
...
...
@@ -628,8 +632,8 @@ uses :class:`~django.core.validators.EmailValidator` to validate the input.
A file-upload field.
.. note::
The ``primary_key`` a
nd ``unique`` arguments are not supported, and will
raise a ``TypeError`` if
used.
The ``primary_key`` a
rgument isn't supported and will raise a an error if
used.
Has two optional arguments:
...
...
docs/releases/1.11.txt
Dosyayı görüntüle @
ec9ed074
...
...
@@ -312,6 +312,9 @@ Models
* Added support for query expressions on lookups that take multiple arguments,
such as ``range``.
* You can now use the ``unique=True`` option with
:class:`~django.db.models.FileField`.
Requests and Responses
~~~~~~~~~~~~~~~~~~~~~~
...
...
tests/invalid_models_tests/test_ordinary_fields.py
Dosyayı görüntüle @
ec9ed074
...
...
@@ -440,21 +440,6 @@ class FileFieldTests(SimpleTestCase):
expected
=
[]
self
.
assertEqual
(
errors
,
expected
)
def
test_unique
(
self
):
class
Model
(
models
.
Model
):
field
=
models
.
FileField
(
unique
=
False
,
upload_to
=
'somewhere'
)
field
=
Model
.
_meta
.
get_field
(
'field'
)
errors
=
field
.
check
()
expected
=
[
Error
(
"'unique' is not a valid argument for a FileField."
,
obj
=
field
,
id
=
'fields.E200'
,
)
]
self
.
assertEqual
(
errors
,
expected
)
def
test_primary_key
(
self
):
class
Model
(
models
.
Model
):
field
=
models
.
FileField
(
primary_key
=
False
,
upload_to
=
'somewhere'
)
...
...
tests/model_fields/models.py
Dosyayı görüntüle @
ec9ed074
...
...
@@ -216,7 +216,7 @@ class DataModel(models.Model):
class
Document
(
models
.
Model
):
myfile
=
models
.
FileField
(
upload_to
=
'unused'
)
myfile
=
models
.
FileField
(
upload_to
=
'unused'
,
unique
=
True
)
###############################################################################
# ImageField
...
...
tests/model_fields/test_filefield.py
Dosyayı görüntüle @
ec9ed074
...
...
@@ -4,6 +4,7 @@ import unittest
from
django.core.files
import
temp
from
django.core.files.uploadedfile
import
TemporaryUploadedFile
from
django.db.utils
import
IntegrityError
from
django.test
import
TestCase
,
override_settings
from
.models
import
Document
...
...
@@ -61,6 +62,15 @@ class FileFieldTests(TestCase):
Document
.
objects
.
create
(
myfile
=
'something.txt'
)
self
.
assertEqual
(
Document
.
objects
.
defer
(
'myfile'
)[
0
]
.
myfile
,
'something.txt'
)
def
test_unique_when_same_filename
(
self
):
"""
A FileField with unique=True shouldn't allow two instances with the
same name to be saved.
"""
Document
.
objects
.
create
(
myfile
=
'something.txt'
)
with
self
.
assertRaises
(
IntegrityError
):
Document
.
objects
.
create
(
myfile
=
'something.txt'
)
@unittest.skipIf
(
sys
.
platform
.
startswith
(
'win'
),
"Windows doesn't support moving open files."
)
# The file's source and destination must be on the same filesystem.
@override_settings
(
MEDIA_ROOT
=
temp
.
gettempdir
())
...
...
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