From 56e8e969632e39077465780ffd4a15971f304681 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabor=20K=C3=B6rber?= Date: Wed, 2 Jun 2021 10:52:48 +0200 Subject: [PATCH] updating readme --- blackmesa/records/readme.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/blackmesa/records/readme.md b/blackmesa/records/readme.md index de34958..a635ed8 100644 --- a/blackmesa/records/readme.md +++ b/blackmesa/records/readme.md @@ -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`. +