0

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

  • Can you share the code where you're filtering? If I'm understanding your question correctly, why don't you just use an if statement to check if the form value is none before filtering? – Jens Astrup Jul 28 '16 at 11:20
  • @JensAstrup because, if to use "if" i need to do 4 queries different in 1 field, and I want to know maybe there is another way –  Jul 28 '16 at 11:24
  • Can you add the view where the form is being handled? Much easier to understand with the full picture... – Jens Astrup Jul 28 '16 at 11:34
  • @JensAstrup ok, I added –  Aug 28 '16 at 21:25
  • Instead of directly passing in values to the function, you could construct a dict first, [filter the None values](https://stackoverflow.com/questions/2544710/how-i-can-get-rid-of-none-values-in-dictionary), then pass in the remaining values using the `**` call syntax. – Two-Bit Alchemist Aug 28 '16 at 22:06
  • Another solution would be to [write a custom manager](https://stackoverflow.com/questions/4762524/is-it-possible-to-override-objects-on-a-django-model) for this whose `filter` removed arguments which were `None` before calling `super()`. – Two-Bit Alchemist Aug 28 '16 at 22:07

1 Answers1

0

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