diff --git a/django_db_comments/db_comments.py b/django_db_comments/db_comments.py index 3771224..fda305f 100644 --- a/django_db_comments/db_comments.py +++ b/django_db_comments/db_comments.py @@ -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 diff --git a/tests/test_db_comments.py b/tests/test_db_comments.py index c316690..09c2fb9 100644 --- a/tests/test_db_comments.py +++ b/tests/test_db_comments.py @@ -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__(