update readme, bump version

This commit is contained in:
collerek
2020-09-24 16:32:34 +02:00
parent da05063e8d
commit 29bcbae35d
4 changed files with 76 additions and 25 deletions

View File

@ -264,6 +264,52 @@ await news.posts.clear()
``` ```
Since version >=0.3.4 Ormar supports also queryset level delete and update statements
```python
import databases
import ormar
import sqlalchemy
database = databases.Database("sqlite:///db.sqlite")
metadata = sqlalchemy.MetaData()
class Book(ormar.Model):
class Meta:
tablename = "books"
metadata = metadata
database = database
id: ormar.Integer(primary_key=True)
title: ormar.String(max_length=200)
author: ormar.String(max_length=100)
genre: ormar.String(max_length=100, default='Fiction', choices=['Fiction', 'Adventure', 'Historic', 'Fantasy'])
await Book.objects.create(title='Tom Sawyer', author="Twain, Mark", genre='Adventure')
await Book.objects.create(title='War and Peace', author="Tolstoy, Leo", genre='Fiction')
await Book.objects.create(title='Anna Karenina', author="Tolstoy, Leo", genre='Fiction')
await Book.objects.create(title='Harry Potter', author="Rowling, J.K.", genre='Fantasy')
await Book.objects.create(title='Lord of the Rings', author="Tolkien, J.R.", genre='Fantasy')
# update accepts kwargs that are used to update queryset model
# all other arguments are ignored (argument names not in own model table)
await Book.objects.filter(author="Tolstoy, Leo").update(author="Lenin, Vladimir") # update all Tolstoy's books
all_books = await Book.objects.filter(author="Lenin, Vladimir").all()
assert len(all_books) == 2
# delete accepts kwargs that will be used in filter
# acting in same way as queryset.filter(**kwargs).delete()
await Book.objects.delete(genre='Fantasy') # delete all fantasy books
all_books = await Book.objects.all()
assert len(all_books) == 3
# queryset needs to be filtered before deleting to prevent accidental overwrite
# to update whole database table each=True needs to be provided as a safety switch
await Book.objects.update(each=True, genre='Fiction')
all_books = await Book.objects.filter(genre='Fiction').all()
assert len(all_books) == 3
```
## Data types ## Data types
The following keyword arguments are supported on all field types. The following keyword arguments are supported on all field types.

View File

@ -26,7 +26,7 @@ class UndefinedType: # pragma no cover
Undefined = UndefinedType() Undefined = UndefinedType()
__version__ = "0.3.3" __version__ = "0.3.4"
__all__ = [ __all__ = [
"Integer", "Integer",
"BigInteger", "BigInteger",

View File

@ -28,7 +28,9 @@ class ModelTableProxy:
@classmethod @classmethod
def extract_db_own_fields(cls) -> set: def extract_db_own_fields(cls) -> set:
related_names = cls._extract_related_names() related_names = cls._extract_related_names()
self_fields = {name for name in cls.Meta.model_fields.keys() if name not in related_names} self_fields = {
name for name in cls.Meta.model_fields.keys() if name not in related_names
}
return self_fields return self_fields
@classmethod @classmethod

View File

@ -140,20 +140,23 @@ class QuerySet:
self_fields = self.model_cls.extract_db_own_fields() self_fields = self.model_cls.extract_db_own_fields()
updates = {k: v for k, v in kwargs.items() if k in self_fields} updates = {k: v for k, v in kwargs.items() if k in self_fields}
if not each and not self.filter_clauses: if not each and not self.filter_clauses:
raise QueryDefinitionError('You cannot update without filtering the queryset first. ' raise QueryDefinitionError(
'If you want to update all rows use update(each=True, **kwargs)') "You cannot update without filtering the queryset first. "
"If you want to update all rows use update(each=True, **kwargs)"
)
expr = FilterQuery(filter_clauses=self.filter_clauses).apply( expr = FilterQuery(filter_clauses=self.filter_clauses).apply(
self.table.update().values(**updates) self.table.update().values(**updates)
) )
# print(expr.compile(compile_kwargs={"literal_binds": True}))
return await self.database.execute(expr) return await self.database.execute(expr)
async def delete(self, each: bool = False, **kwargs: Any) -> int: async def delete(self, each: bool = False, **kwargs: Any) -> int:
if kwargs: if kwargs:
return await self.filter(**kwargs).delete() return await self.filter(**kwargs).delete()
if not each and not self.filter_clauses: if not each and not self.filter_clauses:
raise QueryDefinitionError('You cannot delete without filtering the queryset first. ' raise QueryDefinitionError(
'If you want to delete all rows use delete(each=True)') "You cannot delete without filtering the queryset first. "
"If you want to delete all rows use delete(each=True)"
)
expr = FilterQuery(filter_clauses=self.filter_clauses).apply( expr = FilterQuery(filter_clauses=self.filter_clauses).apply(
self.table.delete() self.table.delete()
) )