I have django model which contains incident_type
and closed
fields. Based on this fields I want to return functions that render some text.
Is it okay to have such get_renderer
method in models or I should move this logic somewhere else? In my understanding, this method violates single responsibility principle.
class Incident(models.Model):
BAD_RESPONSE = 'bad_response'
SLOW_RESPONSE = 'slow_response'
INCIDENT_TYPE_CHOICES = (
(BAD_RESPONSE, 'Webpage is not available'),
(SLOW_RESPONSE, 'Webpage is slow'),
)
incident_type = models.CharField(max_length=50, choices=INCIDENT_TYPE_CHOICES)
closed = models.BooleanField()
def get_renderer(self):
if self.incident_type == self.BAD_RESPONSE:
if self.closed:
return render_page_up_screen
else:
return render_page_down_screen
I would think it would be more appropriate to locate this method somewhere in your view layer. Keeping your model loosely coupled from web and rendering related concepts is generally good practice. It keeps the model flexible and more adaptable to change.
Moving get_renderer()
into your view should be straightforward? It would still have access to the indicdent_type
and closed
fields via an Indcident
instance to make its rendering decision.