lundi 14 avril 2014

Meilleure pratique : où dois-je écrire des fonctions (pas de méthodes) dans Django ? -Débordement de pile


This is maybe not a fundamental question, but as curious as I am, I'm asking this myself for a long time.


A part of Django code is functions and is not directly related to Django models. Let's take for example this function :


def make_random_raw_password(length=8):
# Do some stuff and return a 8 char randomised string

Technically, this function is not related to the User class, but functionally, it is!


Where would you write this function? From my beginner point of view, I can see at least 2 options:



  1. I create a myutils.py file which contains all these kind of functions then I import myutils.py in files like models.py / view.py to use these functions.


  2. I move this function as a classmethod in my UserProxy class (since I can not directly modify the User class methods in Django) which gives me something close to:



-


class UserProxy(User):
class Meta(object):
proxy = True

@classmethod
def make_random_raw_password(cls, length=8):
# Do some stuff...

Which one would you use? Could you explain why? Thank you in advance.



This is maybe not a fundamental question, but as curious as I am, I'm asking this myself for a long time.


A part of Django code is functions and is not directly related to Django models. Let's take for example this function :


def make_random_raw_password(length=8):
# Do some stuff and return a 8 char randomised string

Technically, this function is not related to the User class, but functionally, it is!


Where would you write this function? From my beginner point of view, I can see at least 2 options:



  1. I create a myutils.py file which contains all these kind of functions then I import myutils.py in files like models.py / view.py to use these functions.


  2. I move this function as a classmethod in my UserProxy class (since I can not directly modify the User class methods in Django) which gives me something close to:



-


class UserProxy(User):
class Meta(object):
proxy = True

@classmethod
def make_random_raw_password(cls, length=8):
# Do some stuff...

Which one would you use? Could you explain why? Thank you in advance.


0 commentaires:

Enregistrer un commentaire