# Form with related fields?

**URL:** <https://forum.djangoproject.com/t/form-with-related-fields/32954>\
**Category:** Forms & APIs\
**Created:** [July 13, 2024, 8:25pm UTC](https://forum.djangoproject.com/t/form-with-related-fields/32954 "2024-07-13T20:25:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![doom](https://avatars.discourse-cdn.com/v4/letter/d/a587f6/32.png) [@doom](https://forum.djangoproject.com/u/doom)\
**Post date:** [July 13, 2024, 8:25pm UTC](https://forum.djangoproject.com/t/form-with-related-fields/32954/1 "2024-07-13T20:25:45Z")

</div>

I have a model `Account` with a 1:1-relation to a Django `User`.

```auto
class Account(models.Model):
    user = OneToOneField(User, on_delete=models.CASCADE)
    description = CharField(max_length=255)

    def save(self, *args, **kwargs):
        secret = get_random_string(32)
        user = User.objects.create_user(
            username=uuid.uuid4(),
            password=make_password(secret, salt="<MYSALT>"),
        )
        self.secret_partial = secret[-3:]
        self.user = user
        super().save(*args, **kwargs)

```

and this `ModelForm`

```auto
class AccountForm(ModelForm):
    class Meta:
        model = Account
        fields = ["description"]

```

The idea is that someone can create an (API) Account that automatically creates a user with a password with a specific salt (so I can later to a lookup by API Secret, but that’s not important here).

I’d like user to pick a description, which is a field of `Account`, but also pick permissions from a limited set.

If I would try to describe what I want to do in code, it would be this:

```auto
class AccountForm(ModelForm):
    class Meta:
        model = Account
        fields = ["description", "user__user_permissions"]

    def __init__ (self, *args, **kwargs):
        super(). __init__ (*args, **kwargs)
        api_permissions = ["can_manage_api_accounts"]
        self.fields["user_permissions"].queryset = Permission.objects.filter(
            codename__in=api_permissions
        )

```

This doesn’t work, of course. But how would I do such a thing?

---

<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 14, 2024, 12:25am UTC](https://forum.djangoproject.com/t/form-with-related-fields/32954/2 "2024-07-14T00:25:00Z")

</div>

You’re on the right track. But the references to permissions is a many-to-many reference and not a field on the model. As a result, you should be creating a non-model form field - perhaps named `permissions`, as a [ModelMultipleChocieField](https://docs.djangoproject.com/en/5.0/ref/forms/fields/#modelmultiplechoicefield). It’s the queryset and content for _that_ field that you can filter.

---

<div class="post-metadata">

**Author:** ![white-seolpyo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/white-seolpyo/32/22386_2.png) [@white-seolpyo](https://forum.djangoproject.com/u/white-seolpyo)\
**Post date:** [July 14, 2024, 5:33am UTC](https://forum.djangoproject.com/t/form-with-related-fields/32954/3 "2024-07-14T05:33:44Z")

</div>

You are calling a field (user\_\_user\_permissions) that does not exist in the model.

You must create an many to many field named user\_\_user\_permissions in the model, or create a separate form field.

The model form basically imports the fields of the inherited model.

If you want to write a field that is not included in the model, you must write it directly in the form.

> **[Creating forms from models | Django documentation](https://docs.djangoproject.com/en/5.0/topics/forms/modelforms/)**
>
> The web framework for perfectionists with deadlines.

---

<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 14, 2024, 11:34am UTC](https://forum.djangoproject.com/t/form-with-related-fields/32954/4 "2024-07-14T11:34:15Z")

</div>

> [@white-seolpyo](#):
>
> You must create an many to many field named user\_\_user\_permissions in the model, or create a separate form field.

This is an invalid suggestion. You cannot create a model field by this name. (It’s illegal to have consecutive underscores within a field name. See [Field name restrictions](https://docs.djangoproject.com/en/5.0/topics/db/models/#field-name-restrictions))
