Python Question

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

I have model with blank=True fields. In my form I have fields that are optional if in model they are blank=True. So, if in my form I want to make them empty, and then I want to filter my model objects, I don't want to use empty fields to search. Do I need to create other query in this 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 my models there is more fields, all of them you can see in views when I save them

Answer

The situation is that I forget to do makemigrations. After that all works fine!