Skip to content

Commit

Permalink
Fixed #27377 -- Clarified that prepopulated_fields doesn't work with …
Browse files Browse the repository at this point in the history
…OneToOneField.
  • Loading branch information
HenryDangPRG authored and timgraham committed Dec 19, 2016
1 parent 3e43d24 commit 6af23a4
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ answer newbie questions, and generally made Django that much better:
Hawkeye
Helen Sherwood-Taylor <[email protected]>
Henrique Romano <[email protected]>
Henry Dang <[email protected]>
[email protected]
Hiroki Kiyohara <[email protected]>
Honza Král <[email protected]>
Expand Down
2 changes: 1 addition & 1 deletion django/contrib/admin/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ def _check_prepopulated_fields_key(self, obj, model, field_name, label):
return [
checks.Error(
"The value of '%s' refers to '%s', which must not be a DateTimeField, "
"a ForeignKey, or a ManyToManyField." % (label, field_name),
"a ForeignKey, a OneToOneField, or a ManyToManyField." % (label, field_name),
obj=obj.__class__,
id='admin.E028',
)
Expand Down
4 changes: 2 additions & 2 deletions docs/ref/checks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ with the admin site:
* **admin.E027**: The value of ``prepopulated_fields`` refers to
``<field name>``, which is not an attribute of ``<model>``.
* **admin.E028**: The value of ``prepopulated_fields`` refers to
``<field name>``, which must not be a ``DateTimeField``, a ``ForeignKey``, or a
``ManyToManyField`` field.
``<field name>``, which must not be a ``DateTimeField``, a ``ForeignKey``,
a ``OneToOneField``, or a ``ManyToManyField`` field.
* **admin.E029**: The value of ``prepopulated_fields[<field name>]`` must be a
list or tuple.
* **admin.E030**: The value of ``prepopulated_fields`` refers to
Expand Down
2 changes: 1 addition & 1 deletion docs/ref/contrib/admin/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ subclass::
slug (e.g. substituting dashes for spaces).

``prepopulated_fields`` doesn't accept ``DateTimeField``, ``ForeignKey``,
nor ``ManyToManyField`` fields.
``OneToOneField``, and ``ManyToManyField`` fields.

.. attribute:: ModelAdmin.preserve_filters

Expand Down
1 change: 1 addition & 0 deletions tests/modeladmin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ValidationTestModel(models.Model):
is_active = models.BooleanField(default=False)
pub_date = models.DateTimeField()
band = models.ForeignKey(Band, models.CASCADE)
best_friend = models.OneToOneField(User, models.CASCADE)
# This field is intentionally 2 characters long (#16080).
no = models.IntegerField(verbose_name="Number", blank=True, null=True)

Expand Down
15 changes: 13 additions & 2 deletions tests/modeladmin/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1006,8 +1006,8 @@ class ValidationTestModelAdmin(ModelAdmin):

self.assertIsInvalid(
ValidationTestModelAdmin, ValidationTestModel,
("The value of 'prepopulated_fields' refers to 'users', which must not be "
"a DateTimeField, a ForeignKey, or a ManyToManyField."),
"The value of 'prepopulated_fields' refers to 'users', which must not be "
"a DateTimeField, a ForeignKey, a OneToOneField, or a ManyToManyField.",
'admin.E028')

def test_valid_case(self):
Expand All @@ -1016,6 +1016,17 @@ class ValidationTestModelAdmin(ModelAdmin):

self.assertIsValid(ValidationTestModelAdmin, ValidationTestModel)

def test_one_to_one_field(self):
class ValidationTestModelAdmin(ModelAdmin):
prepopulated_fields = {'best_friend': ('name',)}

self.assertIsInvalid(
ValidationTestModelAdmin, ValidationTestModel,
"The value of 'prepopulated_fields' refers to 'best_friend', which must not be "
"a DateTimeField, a ForeignKey, a OneToOneField, or a ManyToManyField.",
'admin.E028'
)


class ListDisplayTests(CheckTestCase):

Expand Down

0 comments on commit 6af23a4

Please sign in to comment.