1

I want to call a self method from django model field for get choice options.

I search over stackoverflow as well as google and find some solution but these solution not working in my case. here is some of solution already exist. Django: Call self function inside a Django model.

Here is my model :

MPL_CHOICE = (
    ("a", "assistance"),
    ("b", "Performed"),
    ("n", "Need assistance"),
    ("o", "Observed"),
)

class ProcedurName(models.Model):
    my_choices = models.CharField(
        "My Choices", 
        max_length=100, 
        null=False, 
        blank=False, 
        choices=self.mpl_choice_filed()
    )
    def mpl_choice_filed(self):
        if self.pk:
            avrage_acore = self.avrage_acore.all()
            MPL_CHOICE_NEW = [(m.value,m.label) for m in minimun_avrage_acore]
            return MPL_CHOICE_NEW
        else:
            return MPL_CHOICE


class ChoicesAfterEdited(models.Model):
    label = models.CharField(
            "MinimunAvrageScore Lavel",
            max_length=250,
        )
    value = models.CharField(
            "MinimunAvrageScore Value",
            max_length=250,
        )
    procedure = models.ForeignKey(
        ProcedureSetup, 
        null=True, blank=True, 
        related_name='avrage_acore'
    )

If you see into model, in my_choices field I try to call self.mpl_choice_filed method but I am getting this error.

NameError: name 'self' is not defined

Please help me to find out solution.

Community
  • 1
  • 1
Yogesh dwivedi Geitpl
  • 4,252
  • 2
  • 20
  • 34

1 Answers1

1

self is not available at the time of Model class definition.

Anyway you can not have different choices for different instances of ProcedurName model. You should manipulate with the choices somewhere in the form handling.

catavaran
  • 44,703
  • 8
  • 98
  • 85