python - if form field is None don't find in model in that field -


i have model blank=true fields. in form have fields optional if in model blank=true. so, if in form want make them empty, , want filter model objects, don't want use empty fields search. need create other query in case?

forms.py

class searchgoods(forms.form):     region_from = forms.modelchoicefield(required=false, queryset = region.objects.all(), widget = forms.select())     region_to = forms.modelchoicefield(required=false, queryset = region.objects.all(), widget = forms.select()) 

models.py

class add_good(models.model):     loading_region = models.foreignkey(region, blank=true, related_name="loading_region", null=true)      unloading_region = models.foreignkey(region, blank=true, related_name="unloading_region", null=true) 

views.py

if form['region_from'] == none:             if form['region_to'] == none:                 data_from_db = add_good.objects.filter(loading_country=form['country_from'],                                                unloading_country=form['country_to'],                                                 loading_city=form['city_from'],                                                unloading_city=form['city_to'],                                                 loading_goods_date_from__gte=form['date_from'],                                                loading_goods_date_to__lte=form['date_to'],                                                 mass__gte=form["mass_from"],                                                mass__lte=form["mass_to"],                                                 volume__gte=form['volume_from'],                                                volume__lte=form['volume_to'],                                                 auto_current_type__in=auto_types,                                                )             else:                 data_from_db = add_good.objects.filter(loading_country=form['country_from'],                                                        unloading_country=form['country_to'],                                                         loading_city=form['city_from'],                                                        unloading_city=form['city_to'],                                                         unloading_region=form["region_to"],                                                         loading_goods_date_from__gte=form['date_from'],                                                        loading_goods_date_to__lte=form['date_to'],                                                         mass__gte=form["mass_from"],                                                        mass__lte=form["mass_to"],                                                         volume__gte=form['volume_from'],                                                        volume__lte=form['volume_to'],                                                         auto_current_type__in=auto_types,                                                        )          else:             if form['region_to'] == none:                 data_from_db = add_good.objects.filter(loading_country=form['country_from'],                                                    unloading_country=form['country_to'],                                                     loading_city=form['city_from'],                                                    unloading_city=form['city_to'],                                                     loading_region=form["region_from"],                                                     loading_goods_date_from__gte=form['date_from'],                                                    loading_goods_date_to__lte=form['date_to'],                                                     mass__gte=form["mass_from"],                                                    mass__lte=form["mass_to"],                                                     volume__gte=form['volume_from'],                                                    volume__lte=form['volume_to'],                                                     auto_current_type__in=auto_types,                                                    )             else:                 data_from_db = add_good.objects.filter(loading_country=form['country_from'],                                                        unloading_country=form['country_to'],                                                         loading_city=form['city_from'],                                                        unloading_city=form['city_to'],                                                         loading_region=form["region_from"],                                                        unloading_region=form["region_to"],                                                         loading_goods_date_from__gte=form['date_from'],                                                        loading_goods_date_to__lte=form['date_to'],                                                         mass__gte=form["mass_from"],                                                        mass__lte=form["mass_to"],                                                         volume__gte=form['volume_from'],                                                        volume__lte=form['volume_to'],                                                         auto_current_type__in=auto_types,                                                        ) 

well, exactly, in models there more fields, of them can see in views when save them

the situation forget makemigrations. after works fine!


Comments

Popular posts from this blog

Combining PHP Registration and Login into one class with multiple functions in one PHP file -

Android volley - avoid multiple requests of the same kind to the server? -

magento2 - Magento 2 admin grid add filter to collection -