# ModelForm with field depending on user permission?

**URL:** <https://forum.djangoproject.com/t/modelform-with-field-depending-on-user-permission/31828>\
**Category:** Forms & APIs\
**Created:** [June 7, 2024, 5:24pm UTC](https://forum.djangoproject.com/t/modelform-with-field-depending-on-user-permission/31828 "2024-06-07T17:24:56Z")\
**Posts on this page:** 1\
**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:** [June 7, 2024, 5:24pm UTC](https://forum.djangoproject.com/t/modelform-with-field-depending-on-user-permission/31828/1 "2024-06-07T17:24:56Z")

</div>

I have a model `Address` with a field and a ModelForm.

```auto
class Address(Model):
    number = PositiveSmallIntegerField(default=1)

```

```auto
class AddressForm(ModelForm):
    class Meta:
        model = Address
        fields = " __all__"

```

How do I add a validator to it, when the form is shown to a user with a specific permission? The form should behave as if the model itself would have a number field like this:

```auto
number = PositiveSmallIntegerField(default=1, validators=[MaxValueValidator(100)])

```

By that I mean that the form should only allow values from 0 to 100, and the backend should validate that the value is inbetween 0 and 100.

The view could pass whether or not to use the limited field like so:

```auto
form = AddressForm(enable_limit=True)

```

And I could do something like this:

```auto
class AddressForm(ModelForm):
    class Meta:
        model = Address
        fields = " __all__"

    def __init__ (self, *args, enable_limit=False, **kwargs):
        if enable_limit:
            self.base_fields["number"].widget.attrs["max"] = 100

```

However, this only updates the widget. The form does complain about values \> 100, but I can go “hack” my way around that and would still not have a validation on the backend.

How would you solve this?
