python - Class Variables vs Methods in Django Class Based Views -
i self-taught amateur ever trying understand fundamentals of python, django , programming in general. i'm looking understand issue having.
so have class
class contractsview(inventoryview): template_name = "contracts.html" page = "contracts" primary_table, secondary_table = build_contracts_tables(**{"commodity": none})
and uses following function:
def build_contracts_tables(**kwargs): print('fire') primary_table_query = purchase.inventory.current_contracts_totals(**kwargs) primary_table_fields = ("total_meats", "total_value", "meat_cost") primary_table_html = build_table([primary_table_query,], *primary_table_fields) if primary_table_query else err_str secondary_table_query = purchase.inventory.current_contracts(**kwargs) secondary_table_fields = ("invoice", "supplier", "variety", "meats", "value", "meat_cost", "ship_date") secondary_table_html = build_table(secondary_table_query, *secondary_table_fields) if secondary_table_query else err_str return primary_table_html, secondary_table_html
somehow, view sending template, render data. however, data doesn't update right away (it does), meaning refresh after changes database, old data persist. additionally, don't ever see print
appear in console.
however, when convert class variables functions, works fine:
class contractsview(inventoryview): template_name = "contracts.html" page = "contracts" def primary_table(self): x,y = build_contracts_tables(**{"commodity": none}) return x def secondary_table(self): x, y = build_contracts_tables(**{"commodity": none}) return y
could me understand rule breaking in original attempt?
you shouldn't set primary_table
, secondary_table
class variables, because calculated once when module loaded.
as have worked out, correct approach use methods. way, method runs when view runs, date value.
Comments
Post a Comment