arcee123
arcee123

Reputation: 243

How do I tie a choices option in a django model class to a function

I have this model with an outside function:

def dtChoices(self):
    language = dbModels.objects.filter(pk=self.dbTable.dbSchema.dbModel).values_list('DbType__DbType', flat=True)
    enumerations = []
    datatypes = RefDatatype.objects.values_list('pk', language).all()
    for dt in datatypes:
        enumerations.append((dt[0],dt[1]))
    return enumerations

class dbColumn(models.Model):
    ColumnName = models.CharField(max_length=255)
    dbTable = models.ForeignKey(dbTable, related_name='column')
    Nullable = models.BooleanField(default=True)
    DataType = models.IntegerField(choices=dtChoices(self=self))
    DataLength = models.IntegerField(null=True, default=255)
    DataPrecision = models.IntegerField(default=0, null=True)

The hierarchy goes dbColumn->dbTable

in forms, admin, and DRF, I have to select the dbTable. Without knowing that first of all, I need to insatiate a dbColumn field, and select from a list, based on the list generated from the output. the output requires a parameter from the parent table which is linked by dbTable field

How do I get what I need from the choices for the dbColumn field?

Upvotes: 0

Views: 89

Answers (1)

kia
kia

Reputation: 828

self is not accessible where you are using. You need to use Django Forms and customize the choices for the form field based on selected dbTable

Upvotes: 1

Related Questions