Breaking News

python – how to search Parent object using attribute of a child object, django-filter-Exceptionshub

Questions:

Context: i have a Bearing with a loot of measurements (interior, exterior, etc), but every brand put a number on that same Bearing (same combination of measurements) that is called “Equivalent”. so a bearing can have one, none o many equivalents.
Something like this:

BD Model

Now i want to filter information from all Bearing atributes, but also for the equivalent Number
SQL Example:

select * from bearing where (any atribute of bearing) = x;

but also be able to do this

select * from bearing inner join equivalent where equivalent.number = x; 

I have searcher like this:

enter image description here
enter image description here

filters.py:

class BearingFilter(django_filters.FilterSet):
    class Meta:
        model = Bearing
        fields = [
            'D_INT_A',
            'D_INT_A1',
            'D_EXT_D',
            'D_EXT_D1',
            'D_ESP_B',
            'D_ESP_C',
            'D_ESP_T',
            'N_PARTE_1',
            'N_PARTE_2',
            'FK_TIPO_ROD',
        ]

views.py:

def searcher(request):
    list_Bearing = Bearing.objects.all()
    list_Equiv = Equivalent.objects.all()
    list_Bearing_filter = BearingFilter(request.GET, queryset=list_Bearing)
    return render(request, 'searcher.html', {'equiv': list_Equiv , 'bearing_filter' : list_Bearing_filter })

the html is something like this:

for x in bearing_filter.qs 

    Row with Bearing Info


        Modal With Equivalent info

        for y in equiv
            if x.id = y.Bearing_fk 

                y.Number - y.Brand

            endif
        endFor
endfor

Im still learning django + python, and i don’t know how to approach this. Maybe django-filter is not good in this case i don’t know

How to&Answers:

About rankhawks

Check Also

Python 3 Programming: Beginner To Pro Masterclass

We are on a mission to create the most complete Python programming guide in the …

Learn Social Engineering From Scratch

Basic IT Skills No Linux, programming or hacking knowledge required. Computer with a minimum of …

Complete Python Programming From Basics To Advance Level

Complete Python Programming From Basics To Advance Level Learn python in detail covering Basics and …

Data Visualization On The Browser With Python And Bokeh

Data Visualization On The Browser With Python And Bokeh A complete guide on creating beautiful …

Learn Python – Python Programming For Beginners From Scratch

Learn Python – Python Programming For Beginners From Scratch Python is a highly paid programming …

Leave a Reply

Your email address will not be published. Required fields are marked *