lundi 24 juillet 2017

Design models: search by singleton models in django

I have a model TranslationService, which has some fields and methods which should be overriden by every new service. I have three services: Google, Azure, Bing, which are the only ones in system (we have only one Google service and one Azure service etc.). I need to perform action like this: TranslationService.objects.filter(service_name=service_name).

But if i make TranslationService abstract, i can't iterate over abst class, cause it wasn't created in DB. If I make inheritance I should implement logic of overriding methods in code like model and then create GoogleService via console. Is this the only way or I can reduce creating object via console.

How can I design my models for performing search among translation_services?

class TranslationService(models.Model):
 created_at = models.DateTimeField(auto_now_add=True)
 updated_at = models.DateTimeField(auto_now=True)
 service_name = models.CharField(max_length=40)
 base_url = models.CharField(max_length=255)
 api_version = models.FloatField()

 def get_step_translation(self, pk, lang, lesson):
    pass

 def create_step_translation(self, pk, lang, type):
    pass

class GoogleTranslator(TranslationService):
 base_url = "https://google.com"
 service_name = "Google"
 api_version = 1.5

 def get_step_translation(self, pk, lang, lesson, **kwargs):
  ... some logic
 def create_step_translation(self, pk, lang, type):
  ... some logic

Aucun commentaire:

Enregistrer un commentaire