0

I have two related models: Plaint and WritOfExecutionTemplate. It is possible to add new template on plaint edit page using admin.TabularInline class. When user create new template from plaint page, I need to fill 2 power of attorney (POA) fields based on current plaint. User allowed to change POAs before template will be saved.

enter image description here

I tried to set callback method to field`s default attribute, but I have problem getting referenced plaint object.

class AbstractGeneratedCaseDocument(AbstractBaseModel):
    class Meta:
        abstract = True

    case = models.ForeignKey(
        verbose_name=_('case'),
        to='Case',
        on_delete=models.PROTECT,
    )
    power_of_attorney = models.ForeignKey(
        verbose_name=_('power of attorney'),
        to='ImportedDocument',
        on_delete=models.PROTECT,
        null=True,
        blank=True,
        related_name='+',
    )
    power_of_attorney_2 = models.ForeignKey(
        verbose_name=_('power of attorney') + ' 2',
        to='ImportedDocument',
        on_delete=models.PROTECT,
        null=True,
        blank=True,
        related_name='+',
    )


class Plaint(AbstractGeneratedCaseDocument):
    pass


class WritOfExecutionTemplate(AbstractBaseModel, WhoDidItMixin):
    plaint = models.ForeignKey(
        to='Plaint',
        verbose_name=_('plaint'),
        on_delete=models.PROTECT
    )

    def get_poa1_from_plaint(self):
        if (self.plaint.power_of_attorney is not None
                and self.plaint.case.plaintiff_representative == self.plaint.power_of_attorney.relative_person):
            poa = self.plaint.power_of_attorney
        else:
            poa = None
        return poa

    def get_poa2_from_plaint(self):
        # The same as for the first power of attorney
        if (self.plaint.power_of_attorney_2 is not None
                and self.plaint.case.plaintiff_representative == self.plaint.power_of_attorney_2.relative_person):
            poa = self.plaint.power_of_attorney_2
        else:
            poa = None
        return poa

    power_of_attorney = models.ForeignKey(
        verbose_name=_('power of attorney'),
        to='ImportedDocument',
        on_delete=models.PROTECT,
        #null=True,
        #blank=True,
        related_name='+',
        default=get_poa1_from_plaint,
    )

    power_of_attorney_2 = models.ForeignKey(
        verbose_name=_('power of attorney') + ' 2',
        to='ImportedDocument',
        on_delete=models.PROTECT,
        null=True,
        blank=True,
        related_name='+',
        default=get_poa2_from_plaint
    )

Admin model:

class WritOfExecutionTemplateAdminInline(admin.TabularInline):
    model = WritOfExecutionTemplate


class PlaintAdmin(admin.ModelAdmin):
    inlines = (
        WritOfExecutionTemplateAdminInline,
    )

Error message:

get_poa1_from_plaint() missing 1 required positional argument: 'self'

What is the right way to make what I need?

Artem
  • 517
  • 1
  • 7
  • 24

0 Answers0