Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions django_db_comments/db_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ def get_comments_for_model(model):
column_comments = {}

for field in model._meta.fields:
# Skip fields defined in parent models since they are stored in
# separate database tables. These fields will be processed when
# handling the parent models.
if field.model != model:
continue

comment = []
# Check if verbose name was not autogenerated, according to django code
# https://github.com/django/django/blob/9681e96/django/db/models/fields/__init__.py#L724
Expand Down
28 changes: 28 additions & 0 deletions tests/test_db_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,34 @@ def test_add_column_comments_to_database(self, mock_connections):
mock_cursor.execute.assert_called()
mock_cursor.execute.assert_called_once_with(query, [comment])

def test_get_comments_for_model_with_inherited_fields(self):
class BaseModel(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
date_of_birth = models.DateField()

class Meta:
app_label = "unit_test"

class InheritingModel(BaseModel):
no_comment = models.TextField()
verbose_name = models.TextField("This is a verbose name")
help_text = models.TextField(
help_text="I really should see this in the database"
)

class Meta:
app_label = "unit_test"

column_comments = get_comments_for_model(InheritingModel)
self.assertDictEqual(
column_comments,
{
"verbose_name": "This is a verbose name",
"help_text": "I really should see this in the database",
},
)

@patch("django_db_comments.db_comments.connections")
def test_add_table_comments_to_database(self, mock_connections):
mock_cursor = mock_connections.__getitem__(
Expand Down