Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix index_together fixer for GIS models module #515

Merged
merged 1 commit into from
Dec 2, 2024
Merged
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
5 changes: 4 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
Changelog
=========

* Fix ``check_constraint_condition`` fixer to work when ``django.contrib.gis.db.models`` is used to import ``CheckConstraint``.
* Make these fixers work when ``django.contrib.gis.db.models`` is used to import objects from ``django.db.models``:

* ``check_constraint_condition``
* ``index_together``

`Issue #513 <https://github.com/adamchainz/django-upgrade/issues/513>`__.

Expand Down
10 changes: 8 additions & 2 deletions src/django_upgrade/fixers/index_together.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,15 @@ def visit_ClassDef(
except IndexError:
indexes = None

if "models" in state.from_imports["django.db"]:
if (
"models" in state.from_imports["django.db"]
or "models" in state.from_imports["django.contrib.gis.db"]
):
index_ref = "models.Index"
elif "Index" in state.from_imports["django.db.models"]:
elif (
"Index" in state.from_imports["django.db.models"]
or "Index" in state.from_imports["django.contrib.gis.db.models"]
):
index_ref = "Index"
else:
return
Expand Down
40 changes: 40 additions & 0 deletions tests/fixers/test_index_together.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,3 +595,43 @@ class Meta:
indexes = [models.Index(fields=["bill", "tail"])]
""",
)


def test_models_imported_gis():
check_transformed(
"""\
from django.contrib.gis.db import models

class Duck(models.Model):
class Meta:
index_together = [["bill", "tail"]]
indexes = []
""",
"""\
from django.contrib.gis.db import models

class Duck(models.Model):
class Meta:
indexes = [models.Index(fields=["bill", "tail"])]
""",
)


def test_index_imported_gis():
check_transformed(
"""\
from django.contrib.gis.db.models import Index

class Duck:
class Meta:
index_together = [["bill", "tail"]]
indexes = []
""",
"""\
from django.contrib.gis.db.models import Index

class Duck:
class Meta:
indexes = [Index(fields=["bill", "tail"])]
""",
)