Skip to content

Commit

Permalink
refact: error type in query field and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
keshavmohta009 committed Apr 8, 2024
1 parent 3d1949c commit 7872642
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 5 deletions.
91 changes: 91 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
## Usage

Discover how to define and utilize database views and materialized views seamlessly within your Django project.

## Installation

Ensure you have Python 3.9 or higher and Django 4.x installed before proceeding with the installation.

You can install the views package via pip:

```bash
pip install dbviews-django
```

### Defining Views

To define views in your Django project, you will first need to ensure that the `dbviews` app is included in your project settings. You can do this by adding `'dbviews'` to the `INSTALLED_APPS` list in your `settings.py` file:

```python
# settings.py

INSTALLED_APPS = [
...
'dbviews',
...
]
```

Once dbviews is included, you can define your views using the provided classes.

You can define your views in either `models.py` or in a separate `views.py` file located within a `models` folder.

Here's how you can structure your Django app:

- yourapp/
models/
__init__.py
models.py
views.py


### Here's an example:
```python
# models/views.py

from dbviews import views

class MyView(views.DbView):
"""
Define your view using DbView class.
"""
view_query = views.QueryField(query="SELECT * FROM my_table WHERE condition = true")
# Other fields....

class MyMaterializedView(views.DbMaterializedView):
"""
Define your materialized view using DbMaterializedView class.
"""
view_query = views.QueryField(query="SELECT * FROM my_table WHERE condition = true")
# Other fields....
```

In the above example, MyView and MyMaterializedView are defined as subclasses of DbView and DbMaterializedView respectively. The view_query attribute specifies the SQL query that defines the view's logic.

To make your models and views accessible from the root of the models folder, you need to import them in the __init__.py file:

```python
# __init__.py

from .models import MyModel # Import your models
from .views import MyView # Import your views
```

To refresh the materialized views you can refresh method.
```python
MyMaterializedView.refresh()
```


### Applying Migrations
After defining your views, you'll need to generate migrations to apply these changes to your database schema. Use Django's `makemigrations` and `migrate` command to generate migration files:

```shell
python manage.py makemigrations # This will all the required migrations
python manage.py migrate # This will create all the views and models based on the migration
```

### Contributing
Contributions are welcome!

If you encounter any issues or have feature requests, please don't hesitate to submit them on [GitHub](https://github.com/keshavmohta09/dbviews-django/).
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dbviews-django"
version = "0.1.0"
version = "0.1.1"
description = "Support of database views and materialized views in django web framework."
authors = ["Keshav Mohta <keshavmohta09@gmail.com>"]
readme = "README.md"
Expand All @@ -9,7 +9,7 @@ packages = [
]

[tool.poetry.dependencies]
python = "^3.9"
python = ">=3.9"
django = ">=4"


Expand Down
2 changes: 2 additions & 0 deletions src/dbviews/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class IncorrectFieldNameError(ValueError):
pass
4 changes: 3 additions & 1 deletion src/dbviews/views/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from django.db.models import Field

from dbviews.exceptions import IncorrectFieldNameError

if TYPE_CHECKING:
from dbviews import views

Expand Down Expand Up @@ -37,7 +39,7 @@ def contribute_to_class(
) -> None:

if name != "view_query":
raise FileNotFoundError(
raise IncorrectFieldNameError(
f"Name of field should be `view_query` instead of `{name}`"
)

Expand Down
2 changes: 0 additions & 2 deletions src/dbviews/views/metaclasses.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from copy import copy, deepcopy

from django.db import models

from dbviews.views.fields import QueryField
Expand Down

0 comments on commit 7872642

Please sign in to comment.