District heating (DH) systems are an important component in the EU strategy to reach the emission goals, since they allow an efficient supply of heat while using the advantages of sector coupling between different energy carriers such as power, heat, gas and biomass. Most DH systems use several different types of units to produce heat for hundreds or thousands of households (e.g. natural gas-fired boilers, electric boilers, biomass-fired units, waste heat from industry, solar thermal units). Furthermore, combined heat and power units units are often included to use the synergy effects of excess heat from electricity production. To address the challenge of providing optimization tools for a vast variety of different system configurations, we propose a generic mixed-integer linear programming formulation for the operational production optimization in DH systems. The model is based on a network structure that can represent different system setups while the underlying model formulation stays the same. Therefore, the model can be used for most DH systems although they might use different combinations of technologies in different system layouts. The mathematical formulation is based on stochastic programming to account for the uncertainty of energy prices and production from non-dispatchable units such as waste heat and solar heat. Furthermore, the model is easily adaptable to different application cases in DH systems such as operational planning, bidding to electricity markets and long-term evaluation. We present results from three real cases in Denmark with different requirements.