# Pass instance pk or user.id into a model field for use in dynamic choices.

**URL:** <https://forum.djangoproject.com/t/pass-instance-pk-or-user-id-into-a-model-field-for-use-in-dynamic-choices/22036>\
**Category:** Forms & APIs\
**Created:** [July 3, 2023, 2:27am UTC](https://forum.djangoproject.com/t/pass-instance-pk-or-user-id-into-a-model-field-for-use-in-dynamic-choices/22036 "2023-07-03T02:27:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![imAsparky](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/imasparky/32/8664_2.png) [@imAsparky](https://forum.djangoproject.com/u/imAsparky)\
**Post date:** [July 3, 2023, 2:27am UTC](https://forum.djangoproject.com/t/pass-instance-pk-or-user-id-into-a-model-field-for-use-in-dynamic-choices/22036/1 "2023-07-03T02:27:31Z")

</div>

Hello Folks,

I am trying to find a way to have a model field with dynamic choices, filtering is based on these three items:

1. `user.id` from the FK in the instance.
2. `model_verbose_name`
3. `field_verbose_name`

This is an overview of my setup using a custom field.

```auto
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.

```auto
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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 3, 2023, 3:03am UTC](https://forum.djangoproject.com/t/pass-instance-pk-or-user-id-into-a-model-field-for-use-in-dynamic-choices/22036/2 "2023-07-03T03:03:56Z")

</div>

When you want the choices to be determined at runtime, and a class-level function isn’t going to work for you, then you want to adjust the options on the field in the ` __init__ ` method of the form.

See the discussion at [How to filter the choices in forms.Select widget?](https://forum.djangoproject.com/t/how-to-filter-the-choices-in-forms-select-widget/19974). (There are a few other discussions here that talk about this as well.)

---

<div class="post-metadata">

**Author:** ![imAsparky](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/imasparky/32/8664_2.png) [@imAsparky](https://forum.djangoproject.com/u/imAsparky)\
**Post date:** [July 3, 2023, 5:05am UTC](https://forum.djangoproject.com/t/pass-instance-pk-or-user-id-into-a-model-field-for-use-in-dynamic-choices/22036/3 "2023-07-03T05:05:31Z")

</div>

Thanks Ken, I will take a look.
