Closed mrrichardasmith closed 2 years ago
def query_params_array(query_var): params = [] for v in vars(query_var): params.append(v) return params
def check_query_one(instance): params = [] params = query_params_array(instance) for p in params: if getattr(instance, p) == None: print(f"{p} is None") else: print(p, getattr(instance, p))
3# Function that takes a query of multiple row and therefore a list of objects, itterates and passes single objects to the function in step 2 but also brings it all together checking if the intial query is None then directing the flow.
def check_query_instance(instance): if instance == None: print("Instance is None") elif hasattr(instance, '__iter__'): print("We hit the itteration check") for i in instance: print(type(i)) check_query_one(i) else: check_query_one(instance)
When we query the database, where the returned object is None or where a parameter within a model object is None we run have a lot of manual if statements repeating over and over.
Create a function that accepts a query object and abstracts the parameters that are within if the object does not show as None.
If the query is not None then it could be a query that returned one row or multiple rows and therefore we would need to assess if the object is itterable and if not combine the parameters from the above step with the object to check if the parameter was None.
If the query us multiple rows and therefore itterable we would need to itterate the object into its component objects and then pass those to the step above that checks the parameter status.
We may then want to broaden this scope to add totals from columns into grand totals but this will be a new story after we have the foundational helper functions created. This will hopefully help to refactor and reduce the number of lines of code.