# How to redefine a display form field so it shows a content with filtring?

**URL:** <https://forum.djangoproject.com/t/how-to-redefine-a-display-form-field-so-it-shows-a-content-with-filtring/2139>\
**Category:** Using Django\
**Created:** [April 27, 2020, 10:50pm UTC](https://forum.djangoproject.com/t/how-to-redefine-a-display-form-field-so-it-shows-a-content-with-filtring/2139 "2020-04-27T22:50:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sokovtlt](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@Sokovtlt](https://forum.djangoproject.com/u/Sokovtlt)\
**Post date:** [April 27, 2020, 10:50pm UTC](https://forum.djangoproject.com/t/how-to-redefine-a-display-form-field-so-it-shows-a-content-with-filtring/2139/1 "2020-04-27T22:50:10Z")

</div>

I have some models classes. And some classes have bounding fields with another class. Each class have the field - author. I whould like that for each user the form bounding field shows only those data which was created these authors.

For example:  
class “TypeJob” has field “author”.  
User Jhon created a TypJob - “Painting”.  
User Alex created a TypJob - “Cleaning”.  
Class “Order” has bounding field - “name\_typ\_job”  
When Jhon whould like create an order, he opens the form and click “name type job” field, he sees both type job: painting and cleaning.  
I whould like that he sees and could choose only “painting”. Because he is it author.

Django 3

---

<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:** [April 27, 2020, 11:06pm UTC](https://forum.djangoproject.com/t/how-to-redefine-a-display-form-field-so-it-shows-a-content-with-filtring/2139/2 "2020-04-27T23:06:04Z")

</div>

In your form you would define a queryset for the field that would filter the selections based upon your criteria.

See the queryset option in the [ModelChoiceField](https://docs.djangoproject.com/en/3.0/ref/forms/fields/#modelchoicefield).

---

<div class="post-metadata">

**Author:** ![Sokovtlt](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@Sokovtlt](https://forum.djangoproject.com/u/Sokovtlt)\
**Post date:** [April 28, 2020, 7:14am UTC](https://forum.djangoproject.com/t/how-to-redefine-a-display-form-field-so-it-shows-a-content-with-filtring/2139/3 "2020-04-28T07:14:07Z")

</div>

Thank you for your answer!  
But I don’t understand what exactly I should do. Please could you help me?  
I wrote that

> ```
> class OrderTestForm(forms.ModelForm):
> class Meta:
> model = Order
> name_job = forms.ModelChoiceField(queryset=None)
> qs = TypJob.objects.filter(author__id=user.id)
> def __init__ (self, *args, **kwargs):
> super(). __init__ (*args, **kwargs)
> self.fields['name_job'].queryset = qs
> 
> ```

But I have

name ‘user’ is not defined

name ‘author\_\_id’ is not defined

---

<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:** [April 28, 2020, 11:40am UTC](https://forum.djangoproject.com/t/how-to-redefine-a-display-form-field-so-it-shows-a-content-with-filtring/2139/4 "2020-04-28T11:40:38Z")

</div>

I will also need to see the view that is using this form.

The form itself isn’t going to have all the information you need. You will need to pass the user object from the request object in the view into the form when you’re creating your instance of that form.

---

<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:** [April 28, 2020, 12:02pm UTC](https://forum.djangoproject.com/t/how-to-redefine-a-display-form-field-so-it-shows-a-content-with-filtring/2139/5 "2020-04-28T12:02:16Z")

</div>

But in general, you’re on the right track.  
Your view is, in part, going to look like this (if you’re using a function-based view instead of a class-based view):

```auto
def my_view(request):
   ...
   form = MyForm(current_user=request.user)
   ...

```

Then your form would (in part) look something like this:

```auto
class MyForm(ModelForm):
    class Meta:
        model = MyModel
        fields = ['a_field', 'another_field']

    a_field = forms.ModelChoiceField(queryset=None)

    def __init__ (self, *args, **kwargs):
        super(). __init__ (*args, **kwargs)
        self.fields['a_field'].queryset = MyOtherModel.objects.filter(user=kwargs['current_user'])

```

---

<div class="post-metadata">

**Author:** ![Sokovtlt](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@Sokovtlt](https://forum.djangoproject.com/u/Sokovtlt)\
**Post date:** [April 28, 2020, 4:02pm UTC](https://forum.djangoproject.com/t/how-to-redefine-a-display-form-field-so-it-shows-a-content-with-filtring/2139/6 "2020-04-28T16:02:40Z")

</div>

views.py

> ```
> class OrderNewBigPrintView(LoginRequiredMixin, CustomSuccessMessageMixin, CreateView):
> model = Order
> template_name = 'new_order_bp.html'
> form_class = OrderForm
> success_url = reverse_lazy('orders')
> success_msg = 'Ok'
> def post(self, request, *args, **kwargs):
> form = self.get_form(current_user=request.user)
> 
> ```

forms.py

> ```
> class OrderForm(forms.ModelForm):
> class Meta:
> model = Order
> fields = ' __all__'
> name_job = forms.ModelMultipleChoiceField(queryset=None)
> def __init__ (self, *args, **kwargs):
> super(). __init__ (*args, **kwargs)
> qs = TypJob.objects.filter(user=kwargs['current_user'])
> self.fields['name_job'].queryset = qs
> 
> ```

I have error  
‘NoneType’ object has no attribute ‘\_prefetch\_related\_lookups’

---

<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:** [April 28, 2020, 4:53pm UTC](https://forum.djangoproject.com/t/how-to-redefine-a-display-form-field-so-it-shows-a-content-with-filtring/2139/7 "2020-04-28T16:53:36Z")

</div>

I think I may need to see your models. Is ‘name\_job’ one of the fields in your Order model?

---

<div class="post-metadata">

**Author:** ![Sokovtlt](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@Sokovtlt](https://forum.djangoproject.com/u/Sokovtlt)\
**Post date:** [April 28, 2020, 6:07pm UTC](https://forum.djangoproject.com/t/how-to-redefine-a-display-form-field-so-it-shows-a-content-with-filtring/2139/8 "2020-04-28T18:07:53Z")

</div>

models.py

> ```
> from django.db import models
> from django.contrib.auth.models import User
> from phonenumber_field.modelfields import PhoneNumberField
> import requests
> 
> class Customer(models.Model):
> '''Клиент и данные клиента'''
> author = models.ForeignKey(User, on_delete = models.CASCADE, verbose_name='Автор клиента', null=True)
> name = models.CharField('название', max_length=200)
> name_full = models.CharField('Ф.И.О.', max_length=250)
> tel_number = PhoneNumberField(verbose_name ='номер телефона', blank=True, null=True)
> email = models.EmailField('почта', max_length=100, blank=True, null=True)
> 
> class Order(models.Model):
> author = models.ForeignKey(User, on_delete = models.CASCADE, verbose_name='Автор заказа', blank=True, null=True)
> number_order = models.CharField('номер заказа', max_length=100)
> date_create = models.DateTimeField(auto_now=True)
> name = models.ForeignKey(Customer, on_delete = models.CASCADE, verbose_name='Заказчик', null=True)
> name_order = models.CharField('название', max_length=200)
> 
> ```

---

<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:** [April 30, 2020, 10:25pm UTC](https://forum.djangoproject.com/t/how-to-redefine-a-display-form-field-so-it-shows-a-content-with-filtring/2139/9 "2020-04-30T22:25:57Z")

</div>

What’s the `get_form` function in your view doing? Is it passing the current\_user through to the OrderForm initializer?

---

<div class="post-metadata">

**Author:** ![Sokovtlt](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@Sokovtlt](https://forum.djangoproject.com/u/Sokovtlt)\
**Post date:** [May 1, 2020, 7:54am UTC](https://forum.djangoproject.com/t/how-to-redefine-a-display-form-field-so-it-shows-a-content-with-filtring/2139/10 "2020-05-01T07:54:14Z")

</div>

Thank you for advice.  
I have resolved this issue.

forms.py

> ```
> class OrderForm(forms.ModelForm):
> class Meta:
> model = Order
> fields = ' __all__'
> def __init__ (self, *args, **kwargs):
> # get 'user' param from kwargs
> user = kwargs.pop('user', None)
> super(). __init__ (*args, **kwargs)
> # set class attrs  
> for field in self.fields.values():
> field.widget.attrs['class'] = 'form-control'
> # set 'name_job' queryset
> self.fields['name_job'].queryset = TypJob.objects.filter(author__id=user.id)
> 
> ```

views.py

> ```
> class OrderCreateView(LoginRequiredMixin, CustomSuccessMessageMixin, CreateView):
> model = Order
> template_name = 'new_order.html'
> form_class = OrderForm
> success_url = reverse_lazy('orders')
> success_msg = 'Ok'
> # this method will pass 'user' as param in kwargs to form __init__ ()
> def get_form_kwargs(self):
> kwargs = super(OrderCreateView, self).get_form_kwargs()
> kwargs['user'] = self.request.user
> return kwargs
> 
> ```
