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

arcee123 Source

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:
    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?



answered 3 years ago kia #1

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

comments powered by Disqus