Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
978 B
2
Indexable
Never
class OrderAdminForm(ModelForm):
    class Meta:
        model = Order
        fields = ('__all__')
        widgets = {
            'street': autocomplete.ModelSelect2(
                url='api_novaposhta:novaposhta:street_filter',
                forward=['city']
            ),
            'department': autocomplete.ModelSelect2(
                url='api_novaposhta:novaposhta:warehouse_np_filter',
                forward=['city']
            )
        }



        path(
            'street-filter/',
            StreetAutocomplete.as_view(),
            name='street_filter'
        ),





from dal import autocomplete
from apps.django_novaposhta.models import Street, Warehouse


class StreetAutocomplete(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        city = self.forwarded.get('city', None)

        qs = Street.objects.all()
        qs = qs.filter(
            description__istartswith=self.q,
            city=city
        )

        return qs