Filter a field in a form CreatView

43 views
Skip to first unread message

Walter Randazzo

unread,
Oct 28, 2020, 9:23:42 AM10/28/20
to Django users
Hi guys, How r u doing?

I have a field called articulos that is foreign key,  in a form is defined as follow:

forms.py
from django import forms
#from django.contrib.auth.models import User
from .models import Stockmov

class StockmovForm(forms.ModelForm):
    class Meta:
        model = Stockmov
        #fields ="__all__"
        fields =  ['articulo', 'motivo', 'Cantidad' ]
        widgets = {
        'articulo': forms.Select(attrs={'class':'form-control', 'placeholder':'Articulo'}),
        'motivo': forms.TextInput(attrs={'class':'form-control', 'placeholder':'Motivo'}),
        'Cantidad': forms.NumberInput(attrs={'class':'form-control', 'placeholder':'Cantidad'}),
        }
        labels = {
            'articulo':'', 'motivo':'', 'Cantidad':'',
        }

I want to allow the user to search an instance in the same  combobox articulo in the createview. Check the pic.

Whats is the best native way to do that?Other alternatives?

Thanks in Advance!
WhatIWant.png

David Nugent

unread,
Oct 29, 2020, 12:25:40 AM10/29/20
to django...@googlegroups.com, Walter Randazzo
This is known as a "combo box", somewhat different to a select.

Check this google search: html5 combo box django

Some useful video tutorials + some explanations on how it all works.

A simpler approach - override the widget class similar to how you have done, but there is no need to override them all and I would avoid doing so. If you just need to add some attributes, you can add to the attrs dict of the existing widget instead of replacing it entirely. Plenty of examples exist for that, including the (excellent) django documentation. For your combo box selector you would need to override the widget in order to render the html so you use something different to Select.

Something like (except from a recent project of mine) the following in the form:

class SomeForm(forms.Form)
...
    HEAR_ABOUT_US = (
        'Celebrant',
        'brochure about us',
        'funeral director',
        'facebook page or post',
        'medical or social worker',
        'priest, pastor or minister',
        'friend or family member',
        'other...',
    )
 ...
    hear_about = forms.CharField(label='Where did you hear about us?', max_length=64, required=False,
                        widget=ListTextWidget(name='hear_about', data_list=HEAR_ABOUT_US),
                        help_text='Please let us know how you heard about us (press down or click arrow for options)')
...


And the widget, which could probably do with some improvement:

class ListTextWidget(forms.TextInput):

    def __init__(self, name, data_list, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._name = name
        self._list = data_list
        self.attrs.update(list=f'list__{self._name}')

    def render(self, name, value, attrs=None, renderer=None):
        text_html = super(ListTextWidget, self).render(name, value, attrs=attrs)
        data_list = f'<datalist id="list__{self._name}">'
        for item in self._list:
            data_list += f'<option value="{item}">'
        data_list += '</datalist>'
        return (text_html + data_list)


HTH, David

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/692f2017-ab9e-40b7-8a7c-5c22bffaf368n%40googlegroups.com.
<WhatIWant.png>

Walter Randazzo

unread,
Oct 29, 2020, 2:45:29 PM10/29/20
to dav...@uniquode.io, django...@googlegroups.com
Hi David,

Thanks so much for your reply. Im checking the  videos at the google search.

So hopefully I'l develop a combobox with that.

Thanks so much sir!

Reply all
Reply to author
Forward
0 new messages