4 ms·
I'm at a crossroads for a hobby project I'm working on. I have a model for youtube video metadata. I have functions to do things like transform the metadata i
by piyh 5y ago
I'm at a crossroads for a hobby project I'm working on. I have a model for youtube video metadata. I have functions to do things like transform the metadata into display values to be rendered to template.
Should that logic be a function that takes a youtube metadata model object, or a class method on the model to return display values?
There are more functions of increasing complexity after this display function.
- Daishiman 5y agoI personally keep display functions away from business objects. You could do with a mixin, or a helper class.
- rajin444 5y agoIf you're not able to quickly answer it, I'd put it where you feel like you'd first look for it (where it feels natural) and move on - don't try to optimize too early. The correct answer is going to depend heavily on what your app is doing, how it's structured, etc, so providing a good answer from an HN comment is going to be really hard. It's ok to be "wrong", just make sure you're consistently wrong so when you need to refactor it's not so bad. Eventually the answer will become clear...and if it never does, you probably made the right choice and saved a lot of time :) FWIW, https://www.django-rest-framework.org/api-guide/serializers/ https://www.django-rest-framework.org/api-guide/serializers/ might be helpful - but DRF is also a lot to take in if you're not already comfortable with Django.
- lastofus 5y agoSounds like you might have a View Model. One option would be to define this View Model that takes your Model in the constructor, then make this view model object available in the template context.