Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

minor fix: handle value error when invalid value is passed during update #32

Merged
merged 2 commits into from
May 24, 2023
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
48 changes: 28 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,32 +60,40 @@ urlpatterns = [
]
...
```
This way your app will be ready to serve the REST API to expose model query, you can request an API like follows:
This way your app will be ready to serve the REST API to expose model query, you can request the API
to perform basic CRUD operations using one of the following URLs
- Create: `create/db_name/app_name/model_name`
- Read: `read/db_name/app_name/model_name/<?pk>`
- Update: `update/db_name/app_name/model_name/pk`
- Delete: `delete/db_name/app_name/model_name/pk`

Example Usage:

```python
params = {
'db_name': 'db1',
'app_name': 'machine', # required
'model_name': 'Machine', # required
'filter': {
'os__name': 'os-name-1'
},
'fields': ['ip', 'name', 'id'],
'exclude': {
'name': 'machine-name-11'
},
'order_by': ['ip'],
'limit': 5,
'offset': 10, # default 0
}
api_url = '<yoursite.com>/api/bridgeql/dj_read'
resp = make_get_api_call(api_url, {'payload': json.dumps(params)})
result = resp.json()
import requests

params = {
'filter': {
'os__name': 'os-name-1'
},
'fields': ['ip', 'name', 'id'],
'exclude': {
'name': 'machine-name-11'
},
'order_by': ['ip'],
'limit': 5,
'offset': 10, # default 0
}
# db_name could be default
api_url = '<yoursite.com>/api/bridgeql/read/default/machine/Machine'
resp = requests.get(api_url, {'payload': json.dumps(params)})
result = resp.json()
```

The above parameters will translate into running the model query for `Machine` model of `machine` django app.

```python
Machine.objects.using('db1')
Machine.objects.using('default')
.filter(os__name = 'os-name-1')
.exclude(name = 'machine-name-11')
.values(['ip', 'name', 'id'])
Expand Down
2 changes: 1 addition & 1 deletion bridgeql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"""

__title__ = 'BridgeQL'
__version__ = '0.2.1'
__version__ = '0.2.2'
__license__ = 'BSD 2-Clause'
__copyright__ = 'Copyright © 2023 VMware, Inc. All rights reserved.'

Expand Down
6 changes: 3 additions & 3 deletions bridgeql/django/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ def update(self, params):
raise InvalidRequest('%s does not have field %s'
% (self.instance._meta.model.__name__,
key))
setattr(self.instance, key, val)
# Perform validation
self.instance.validate_unique()
self.instance.save()
except (ValidationError, IntegrityError, AttributeError) as e:
except (AttributeError, IntegrityError,
ValidationError, ValueError) as e:
raise InvalidRequest(str(e))
return self.instance

Expand All @@ -194,7 +194,7 @@ def create(self, params):
try:
self.instance.validate_unique()
self.instance.save(**save_kwargs)
except (ValidationError, IntegrityError) as e:
except (IntegrityError, ValidationError, ValueError) as e:
raise InvalidRequest(str(e))
return self.instance

Expand Down