updating readme

This commit is contained in:
Gabor Körber 2021-06-02 10:52:48 +02:00
parent 77bb19c962
commit 56e8e96963

View File

@ -98,10 +98,11 @@ class MyRecord:
Building this record, which for now we assume will be a frozen dataclass in the future, would be done like this:
```python
MyModel.objects.filter(...).records(MyRecord, 'data', age=Adjunct(0), parent_name=F('parent__name'), has_data=Lambda(lambda d: d.get('data') is not None))
queryset = MyModel.objects.filter(...)\
.records(MyRecord, 'data', age=Adjunct(0), parent_name=F('parent__name'), has_data=Lambda(lambda d: d.get('data') is not None))
```
This will return an iterator that will contain data like this: `[MyRecord(name=..., age=0, parent_id=..., parent_name=..., has_data=...), ...]`
`list(queryset)` will return data like this: `[MyRecord(name=..., age=0, parent_id=..., parent_name=..., has_data=...), ...]`
What has been done here?
- the field data was not on MyRecord, but needed by has_data, so it was included in the values() call by providing it as a string argument.
@ -110,3 +111,5 @@ What has been done here?
- the parent_name was, just like in values(), a Django Expression as kwarg. it will be written into the appropriate field.
- has_data finally will get it's data by calling the lambda function for each fetch, when the record is instantiated. for that it will extract whether the data entry returned some value from the db, which we included earlier for this reason. Of course this would have been possible with a db call as well, but this is to show the mechanic.
similarly, you could just call `queryset.first()` and retrieve the first `MyRecord` entry, or `None`.