Comments (6)
Hi.
I am wondering why the KeyConstructor bits are class variables not instance?
Why? Instances of what? As I see they are instances. For example:
class DefaultKeyConstructor(KeyConstructor):
unique_method_id = bits.UniqueMethodIdKeyBit()
format = bits.FormatKeyBit()
language = bits.LanguageKeyBit()
unique_method_id
- is instance of bits.UniqueMethodIdKeyBit
.
I think it's a good idea to allow to send custom params
attribute in KeyConstructor
initialization method, but it brings some complexity. For example, if you send params={'request_meta': ['GEOIP_COUNTRY']}
, what about other key bits? Should they use default params, or fail without their custom params?
You can rewrite your example code with simple class inheritance:
class CityView(views.APIView):
@cache_response(key_func=CityGetKeyConstructor())
def get(self, request, *args, **kwargs):
...
class CountryGetKeyConstructor(CityView):
request_meta = RequestMetaKeyBit(['GEOIP_COUNTRY'])
class CountryView(views.APIView):
@cache_response(key_func=CountryGetKeyConstructor())
def get(self, request, *args, **kwargs):
...
But anyway, i think it's a good idea that needs more planning. I would love to review pull request or see more of your use cases.
from drf-extensions.
Yes, but each instance of DefaultKeyConstructor shares the same instances of the KeyBits. If you use the same KeyConstructor in two different places, you cant change the parameters of it's KeyBits.
My actual use case is a little different (similar to the CustomListKeyConstructor in your docs).
class CustomListKeyConstructor(DefaultKeyConstructor):
def __init__(self, models):
self.model_bit = UpdatedAtKeyBit(models)
super(CustomListKeyConstructor, self).__init__()
Where UpdatedAtKeyBit checks a updated_at key. Currently this will not work because DefaultKeyConstructor only looks in dir(self.__class__)
, and I wondered if this was by design
Thanks
from drf-extensions.
You can try to alter self.bits
in __init__
method:
class CustomListKeyConstructor(DefaultKeyConstructor):
def __init__(self, models):
super(CustomListKeyConstructor, self).__init__()
self.bits['model_bit'] = UpdatedAtKeyBit(models)
from drf-extensions.
Great, thanks. Do you think caching based on model last-modified is a common enough use case to provide a KeyConstuctor?
from drf-extensions.
Absolutely. I'm going to implement KeyConstructor params
attribute in next two days and i think it will be a better approach than such kind of __init__
redefinition.
By the way, don't forget to use *args
and **kwargs
in super invocation.
class CustomListKeyConstructor(DefaultKeyConstructor):
def __init__(self, models, *args, **kwargs):
super(CustomListKeyConstructor, self).__init__(*args, **kwargs)
self.bits['model_bit'] = UpdatedAtKeyBit(models)
from drf-extensions.
I've implemented KeyConstructor params
attribute http://chibisov.github.io/drf-extensions/docs/#key-constructor-params
from drf-extensions.
Related Issues (20)
- DRF-extensions queryset use select_related,show different sql result
- UniqueFieldsMixin should not validate field when `required=False` HOT 1
- Bulk create
- Django Cron Abandoned HOT 4
- Not compatible with django 3.2 HOT 1
- NestedViewSetMixin not working in custom actions
- My Cron Job runs twice
- Release compatible with Django 3.2 HOT 3
- "'Response' object has no attribute '_headers'" HOT 4
- Release the package with Django 3.2 support HOT 1
- get children is too slow! cache seem not very high perform
- distutils has been deprecated in Python 3.10 in favour of setuptools HOT 1
- We are getting AttributeError: 'Response' object has no attribute '_headers' HOT 2
- Nested router lookup by multiple fields HOT 1
- 500 error returns in case of invalid parent lookup HOT 3
- cache_response get error: inner() missing 1 required positional argument: 'request'
- Is this project still supported? HOT 5
- Can't get the nested routing to work after reading the docs.
- Will this library still be maintained and have new versions released? HOT 7
- Tests suite is failing with DRF 3.15.0 and beyond
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from drf-extensions.