Metadata-Version: 2.4
Name: django-fox-query
Version: 0.1.0
Summary: Static ORM query analyzer for Django — detects N+1, missing indexes, unused queries and more
Author-email: Thomas <thomas.largilliere20120@gmail.com>
License: MIT License
        
        Copyright (c) 2026 LARGILLIERE Thomas
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/ThomasLargilliere/django-fox-query
Keywords: django,orm,query,performance,n+1,static analysis
Classifier: Development Status :: 3 - Alpha
Classifier: Framework :: Django
Classifier: Framework :: Django :: 4.0
Classifier: Framework :: Django :: 5.0
Classifier: Framework :: Django :: 6.0
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: Django>=4.0
Dynamic: license-file

# django-fox-query 🦊

Static ORM query analyzer for Django. Scans your codebase and detects query performance issues before they hit production.

## Installation

```bash
pip install django-fox-query
```

Add to your `INSTALLED_APPS`:

```python
INSTALLED_APPS = [
    ...
    "django_fox_query",
]
```

## Usage

```bash
python manage.py find_query
```

That's it. No configuration needed, no runtime overhead — it's pure static analysis.

## What it detects

**N+1 queries**

Detects querysets on models that have `ForeignKey`, `OneToOneField` or `ManyToManyField` relations without `select_related()` or `prefetch_related()`.

```python
# ⚠️ detected
books = Book.objects.all()

# ✅ ok
books = Book.objects.all().select_related('author')
```

---

**Missing indexes on filter()**

Cross-references the fields used in `.filter()` and `.exclude()` with the model definition to detect fields without `db_index=True` or `unique=True`.

```python
# ⚠️ detected — title has no index
books = Book.objects.filter(title="Django")
```

---

**Unnecessary field loading**

Tracks which fields are actually accessed on a queryset and suggests `.values()` or `.values_list()` when only a subset is used.

```python
# ⚠️ detected — only author__name is used
books = Book.objects.all()
for book in books:
    print(book.author.name)

# ✅ suggestion
books = Book.objects.values('author__name')
```

---

**Unused querysets**

Detects querysets assigned to a variable that is never used in the scope.

```python
# 🗑️ detected — authors is never used
authors = Author.objects.filter(name="Victor Hugo")
```

---

**`len()` instead of `.count()`**

Detects `len(queryset)` calls which load all objects into memory instead of issuing a single `COUNT` query.

```python
# ⚡ detected
count = len(Book.objects.all())

# ✅ suggestion
count = Book.objects.count()
```

## Example output

```
📄 /app/core/views.py:4
   scope  → my_view
   model  → Book
   method → .objects.all()
   code   → books = Book.objects.all()
   ⚠️  warning → relation 'author' (ForeignKey) → utilise .select_related('author')
   💡 values → seuls 'author__name' sont utilisés → envisage .values('author__name')

📄 /app/core/views.py:5
   scope  → my_view
   model  → Author
   method → .objects.filter()
   code   → authors = Author.objects.filter(name="Victor Hugo")
   🗑️  unused → 'authors' n'est jamais utilisé → query inutile, envisage la suppression
   🔍 filter → filter sur 'name' sans index → ajoute db_index=True sur Author.name

── len() vs count() ──────────────────
📄 /app/core/views.py:12
   code   → count = len(books)
   ⚡ count → len(books) sur un queryset → utilise books.count()

⚠️  3 N+1 potentiel(s) détecté(s)
4 queries trouvées
```

## How it works

`find_query` parses your Python files using the `ast` module — no code is executed, no database connection is needed. It builds a map of your ORM calls and your model definitions, then cross-references the two to surface issues.

Migrations and `site-packages` are automatically ignored.

## Requirements

- Python 3.10+
- Django 4.0+
