Hello Folks,
I am trying to find a way to have a model field with dynamic choices, filtering is based on these three items:
user.id
from the FK in the instance.model_verbose_name
field_verbose_name
This is an overview of my setup using a custom field.
class MyCustomField(JSONField):
def formfield(self, **kwargs):
# Return a MultipleChoiceField with appropriate widget
kwargs["widget"] = forms.SelectMultiple()
return forms.MultipleChoiceField(**kwargs)
My preferred way to do this would be to have the instance primary key
or user.id
available somewhere in MyCustomField
, that way I could override get_choices
and the whole process of using MyCustomField
will be simpler.
I have spent quite some time exploring the field object, getting a better understanding of how it all fits together, but getting the instance ID into the field object has eluded me so far.
Here is the second option I had considered and explored. Likewise this has eluded me.
class MyModel(models.Model):
user = models.ForeignKey(
CustomUser,
on_delete=models.CASCADE,
)
dynamic_choices_field = MyCustomField(
choices=get_choices(
user="How do I get user.id here?",
etc...
)
I would appreciate any thoughts about this, a solution would be amazing
thank you