Skip to content

Commit 7bcc661

Browse files
atodorovclaudep
authored andcommitted
Add db_index=True to object_pk and is_removed fields
alter object_pk field to CharField(max_length=64) for compatibility with MySQL.
1 parent b0a7a9e commit 7bcc661

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

HISTORY.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ Unreleased
88

99
* Dropped support for Django 1.11, 2.0, and 2.1.
1010
* Added the ``delete_stale_comments`` management command.
11+
* Added db_index to ``object_pk`` and ``is_removed`` fields.
12+
* Altered ``object_pk`` from ``TextField`` to ``CharField(max_length=64)``
13+
so that the field can be indexed on MySQL, too. **Warning:** if you attach
14+
comments to objects whose primary key is serialized to more than 64
15+
characters, you should provide a custom Comment model
16+
(more about that in the documentation) with an appropriate
17+
``object_pk`` field.
18+
1119

1220
2.0.0 (2020-12-20)
1321
------------------

django_comments/abstracts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class BaseCommentAbstractModel(models.Model):
2323
verbose_name=_('content type'),
2424
related_name="content_type_set_for_%(class)s",
2525
on_delete=models.CASCADE)
26-
object_pk = models.TextField(_('object ID'))
26+
object_pk = models.CharField(_('object ID'), db_index=True, max_length=64)
2727
content_object = GenericForeignKey(ct_field="content_type", fk_field="object_pk")
2828

2929
# Metadata about the comment
@@ -65,7 +65,7 @@ class CommentAbstractModel(BaseCommentAbstractModel):
6565
is_public = models.BooleanField(_('is public'), default=True,
6666
help_text=_('Uncheck this box to make the comment effectively '
6767
'disappear from the site.'))
68-
is_removed = models.BooleanField(_('is removed'), default=False,
68+
is_removed = models.BooleanField(_('is removed'), default=False, db_index=True,
6969
help_text=_('Check this box if the comment is inappropriate. '
7070
'A "This comment has been removed" message will '
7171
'be displayed instead.'))
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from django.db import migrations, models
2+
3+
4+
class Migration(migrations.Migration):
5+
6+
dependencies = [
7+
('django_comments', '0003_add_submit_date_index'),
8+
]
9+
10+
operations = [
11+
migrations.AlterField(
12+
model_name='comment',
13+
name='is_removed',
14+
field=models.BooleanField(
15+
db_index=True, default=False,
16+
help_text='Check this box if the comment is inappropriate. '
17+
'A "This comment has been removed" message will be displayed instead.',
18+
verbose_name='is removed'),
19+
),
20+
migrations.AlterField(
21+
model_name='comment',
22+
name='object_pk',
23+
field=models.CharField(max_length=64, db_index=True, verbose_name='object ID'),
24+
),
25+
]

docs/models.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ The comment models
2828

2929
.. attribute:: object_pk
3030

31-
A :class:`~django.db.models.TextField` containing the primary
32-
key of the object the comment is attached to.
31+
A :class:`~django.db.models.CharField` containing the primary
32+
key of the object the comment is attached to. ``max_length=64``.
3333

3434
.. attribute:: site
3535

0 commit comments

Comments
 (0)