Django Model’s Methods Django.How

Author avatar wrote on 08/06/2022

FAT models THIN view

1. Can be called from any where by selecting the object (No need to repeat)
2.Can be accessed on templates if they return value (No need to pass to the context)

class Job(models.Model):
    project = models.ForeignKey(Project, on_delete=models.CASCADE)
    source_file_path = models.ForeignKey(File, on_delete=models.CASCADE)
    translation_file_path = models.ForeignKey(FileTranslation, on_delete=models.SET_NULL, null=True, blank=True)

    def __str__(self):
        return str(

    def is_job_ready_to_start(self):
        is_ready = False
        previous_job = None
        if Job.objects.filter(, project=self.project, source_file_path=self.source_file_path).exists():
            previous_job = Job.objects.filter('id').last()
            if previous_job.status == 'AP' or previous_job.status == 'ST':
                is_ready = True
                previous_job = previous_job
            is_ready = True

        return is_ready, previous_job

Returns the job's active assignment(s)

    def assigned_translators(self, get_first=True):
        # this to be used as long as we have 1 translator per job
        assigned_translators = JobAssignment.objects.filter(job=self, is_active=True)

        if not assigned_translators:
            return None

        # if only one (default), return first
        if get_first:
            if assigned_translators:
                assigned_translators = assigned_translators.first()

        return assigned_translators

    def translator(self):
        assignment = self.assigned_translators()
        if assignment:
            return assignment.translator
            return None

    def get_translator_cost(self, translator):
        # If not the job is assignable we return 0
        if not self.sub_service.sub_service.is_assigned:
            return 0, 'Not assignable'

        translator_price_to_pay = 0

1. Calculate translator prices for that pair

        if TranslatorPrice.objects.filter(translator=translator, source_language=self.project.lang_from, target_language=self.target_language).exists():
            translator_price = TranslatorPrice.objects.filter(translator=translator, source_language=self.project.lang_from, target_language=self.target_language).first()
            print('(5) there is translator price')
            if str(self.sub_service) == 'Translation':
                translator_price_to_pay = translator_price.translation
            elif str(self.sub_service) == 'Proofreading':
                translator_price_to_pay = translator_price.proofreading
            elif str(self.sub_service) == 'Post editing':
                translator_price_to_pay = translator_price.post_editing
            # print('(5) there is no translator price, we will use default 0.2')
            translator_price_to_pay = 0

        # if price is 0
        if translator_price_to_pay == 0:
            return 0, 'No price'


2. Check Unit type

        if self.sub_service.sub_service.base_unit == 'WD':
            order_amount = translator_price_to_pay * self.source_file_path.word_count
            order_amount = translator_price_to_pay * self.project.order.quote_price.quote.request.word_count                                
        print('     order_amount', order_amount)

        return order_amount, 'Got price'

    def get_assigned_translator_cost(self):
        if not self.sub_service.sub_service.is_assigned:
            return 0, 'Not assignable'

        if self.assigned_translators():
            active_assignment = self.assigned_translators()
            cost, status = self.get_translator_cost(active_assignment.translator)
            return cost, 'Assigned'
            return 0, 'Not assigned'

In any view you proccess or access any method

job_cost = job.get_translator_cost()

In template you can access job.translator

{{ job.translator }}