File tree 2 files changed +26
-0
lines changed
2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -810,6 +810,23 @@ def method(*args, **kwargs):
810
810
811
811
return None
812
812
813
+ def only (self , attributes : list ) -> dict :
814
+ if isinstance (attributes , str ):
815
+ attributes = [attributes ]
816
+ results : dict [str , Any ] = {}
817
+ for attribute in attributes :
818
+ if " as " in attribute :
819
+ attribute , alias = attribute .split (" as " )
820
+ alias = alias .strip ()
821
+ attribute = attribute .strip ()
822
+ else :
823
+ alias = attribute .strip ()
824
+ attribute = attribute .strip ()
825
+
826
+ results [alias ] = self .get_raw_attribute (attribute )
827
+
828
+ return results
829
+
813
830
def __setattr__ (self , attribute , value ):
814
831
if hasattr (self , "set_" + attribute + "_attribute" ):
815
832
method = getattr (self , "set_" + attribute + "_attribute" )
Original file line number Diff line number Diff line change @@ -186,6 +186,15 @@ def test_force_update_on_model_class(self):
186
186
self .assertIn ("username" , sql )
187
187
self .assertIn ("name" , sql )
188
188
189
+ def test_only_method (self ):
190
+ model = ModelTestForced .hydrate (
191
+ {"id" : 1 , "username" : "joe" , "name" : "Joe" , "admin" : True }
192
+ )
193
+
194
+
195
+ self .assertEquals ({"username" : "joe" }, model .only ("username" ))
196
+ self .assertEquals ({"username" : "joe" }, model .only (["username" ]))
197
+
189
198
def test_model_update_without_changes_at_all (self ):
190
199
model = ModelTest .hydrate (
191
200
{"id" : 1 , "username" : "joe" , "name" : "Joe" , "admin" : True }
You can’t perform that action at this time.
0 commit comments