Skip to content

Update to v0.3.0 #15

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

Merged
merged 2 commits into from
Jun 8, 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
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ Note: this will install [pandas](https://pandas.pydata.org/), which is used with

views.py

```shell
from django_qs2csv import qs_to_csv
```python
from qs2csv import qs_to_csv

from .models import SampleModel

Expand All @@ -52,9 +52,9 @@ def export_csv(request):

### Return type

`qs_to_csv` returns a `django.http.HttpResponse` with the `Content-Type` and `Content-Disposition` headers. Additional headers can be added to the response before returning:
All functions return a `django.http.HttpResponse` with the `Content-Type` and `Content-Disposition` headers. Additional headers can be added to the response before returning:

```shell
```python
...

response = qs_to_csv(my_queryset)
Expand All @@ -65,20 +65,27 @@ response["Another-Header"] = "This is another header for the HttpResponse."

### Parameters

`qs : QuerySet` - **Required**. The QuerySet to be exported as a CSV file. This can be passed as QuerySet\[object], QuerySet\[dict] (values()), or QuerySet\[list\[tuple]] (values_list()). See the note in the [Limitations](#limitations) about QuerySet evaluation.
#### Universal

`header : bool` - Include a header row with field names. **Default: False**
`qs : QuerySet` - **Required**. The QuerySet to be exported as a CSV file. This can be passed as QuerySet\[object], QuerySet\[dict] (values()), or QuerySet\[list\[tuple]] (values_list()). See the note in the [Limitations](#limitations) about QuerySet evaluation.

`filename : str` - The name of the exported CSV file. You do not need to include .csv, it will be added once the filename is evaluated. File names can not end in a period, include the symbols (< > : " / \\ | ? *), or be longer than 251 characters (255 w/ ".csv"). **Default: "export"**

`only : list[str]` - List the field names that you would like to include in the exported file. An empty list will include all fields, other than those in `defer`. Field names listed in both `only` and `defer` will not be included. See the note in the [Limitations](#limitations) section for details how this works with a QuerySet that calls only() / defer(). **Default: []**
`only : list[str]` - List the field names that you would like to include in the exported file. An empty list will include all fields, other than those in `defer`. Field names listed in both `only` and `defer` will not be included. See the note in the [Limitations](#limitations) section for details how this works with a QuerySet that calls only(), defer(), values(), or values_list(). **Default: []**

`defer : list[str]` - List the field names that you do not want to include in the exported file. An empty list will include all fields, or just those mentioned in `only`. Field names listed in both `only` and `defer` will not be included. See the note in the [Limitations](#limitations) section for details how this works with a QuerySet that calls only(), defer(), values(), or values_list(). **Default: []**

`defer : list[str]` - List the field names that you do not want to include in the exported file. An empty list will include all fields, or just those mentioned in `only`. Field names listed in both `only` and `defer` will not be included. See the note in the [Limitations](#limitations) section for details how this works with a QuerySet that calls only() / defer(). **Default: []**
`header : bool` - Include a header row with field names. **Default: False**

`verbose : bool` - Determine if the header row uses the fields' `verbose_name` or just the field names. This only applies when `header=True`. **Default: True**

#### qs_to_csv() and qs_to_csv_pd()

`values : bool` - Only enable this if your QuerySet was already evaluated (no longer lazy) and called values(). You must ensure your fields are properly selected in the original QuerySet, because this will skip applying the `only` and `defer` parameters. **Default: False**

#### qs_to_csv_rel_str()
`values : bool` - Only enable this if the QuerySet is passed to the function after calling values() or values_list(). This will convert the QuerySet back to a list of model objects, instead of a list of dicts/lists. See note in [Limitations](#limitations) for an _IMPORTANT WARNING_ about performance. **Default: False**

### Limitations

If the QuerySet was already evaluated before being passed to `qs_to_csv` then it will be re-evaluated by the function. Depending on the size of the QuerySet, complexity of the query and the database setup, this may add a noticeable delay. It is recommended to monitor the impact of database queries using `django.db.connection.queries` or [django-debug-toolbar](https://django-debug-toolbar.readthedocs.io/en/latest/index.html) during development. If the QuerySet must be evaluated before the function is called, it would be most efficient to use values() with the QuerySet (if possible) then pass `values=True` to `qs_to_csv`.
Expand All @@ -89,6 +96,8 @@ If your QuerySet uses only() / defer() then you must include those same fields i

`ManyToManyField` is not supported.

Passing ``values=True`` to ``qs_to_csv_rel_str()`` will create a new query, checking for primary keys (PKs) that are in a list of all PKs from your original QuerySet. **This will add significant time if your QuerySet is large and will potentially not work**, depending on the size of your QuerySet and your database's capabilities. It is recommended to avoid this by not using values() or values_list() when calling this function. It would be more efficient to create a brand new QuerySet than to do use this. Note: if you make this change, ensure `values` is False or the issue will remain.

## License

This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details.
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ build-backend = "setuptools.build_meta"

[tool.setuptools.packages.find]
where = ["qs2csv\\src"]
include = ["django_qs2csv"]
include = ["qs2csv"]

[project]
name = "django-qs2csv"
version = "0.2.3"
version = "0.3.0"
dependencies = [
"django>=3.2",
'importlib-metadata; python_version<"3.10"',
Expand All @@ -19,7 +19,7 @@ authors = [
]
readme = "README.md"
requires-python = ">=3.9"
keywords = ["csv", "export", "pandas", "httpresponse", "http response", "django"]
keywords = ["django", "csv", "export", "httpresponse", "http response", "pandas"]
classifiers = [
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
Expand Down Expand Up @@ -53,7 +53,7 @@ classifiers = [
pd = ["pandas>=1.5"]

[project.urls]
Repository = "https://github.com/Jer-Pha/django-qs2csv"
Homepage = "https://github.com/Jer-Pha/django-qs2csv"
Issues = "https://github.com/Jer-Pha/django-qs2csv/issues"

[tool.black]
Expand Down
2 changes: 1 addition & 1 deletion qs2csv/src/qs2csv/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .qs2csv import qs_to_csv, qs_to_csv_pd
from .qs2csv import qs_to_csv, qs_to_csv_pd, qs_to_csv_rel_str
Loading
Loading