0

I have problem because I'm using smart select ( 3 select fields )to filter my table. But engine id is wrong and don't get correct query. I need to get no id from db but common names.

views.py

def spec_list(request):
if request.method == 'GET':
    form = SpecForm(request.GET)
    if form.is_valid():
        mark = form.cleaned_data['mark']
        model = form.cleaned_data['model']
        engine = form.cleaned_data['engine']
        all_datap = Spec.objects.filter(mark=mark).filter(model=model).filter(engine=engine)
        return render(request, 'specyfikacja/specyfikacja.html', {'form': form,'all_datap' : all_datap})
else:
    form = SpecForm()
return render(request, 'specyfikacja/specyfikacja.html', {'form': form})

From

mark = form.cleaned_data['mark']

I'm getting id like "1" but need name like "porshe"

models.py

from django.db import models
from smart_selects.db_fields import ChainedForeignKey

class Mark(models.Model):
    mark = models.CharField(max_length = 60)    
    def __unicode__(self):
        return unicode(self.mark)

class Model(models.Model):
    mark = models.ForeignKey(Mark)
    model = models.CharField(max_length = 300)
    def __unicode__(self):
        return unicode(self.model)

class Engine(models.Model):
    model = models.ForeignKey(Model)
    engine = models.CharField(max_length = 300)
    def __unicode__(self):
        return unicode(self.engine)


class Spec(models.Model):
    mark = models.ForeignKey(Mark)
    model = ChainedForeignKey(Model, chained_field = "mark", chained_model_field = "mark", show_all = False, auto_choose= True)
    engine = ChainedForeignKey(Engine, chained_field = "model", chained_model_field = "model", auto_choose= True)
    oe = models.CharField(max_length=4, null=True, blank=True)
    plt = models.CharField(max_length=3, null=True, blank=True)
    szer = models.CharField(max_length=5, null=True, blank=True)
    series = models.CharField(max_length=5, null=True, blank=True)
    zr = models.CharField(max_length=5, null=True, blank=True)
    r = models.CharField(max_length=2, null=True, blank=True)
    rim = models.CharField(max_length=5, null=True, blank=True)
    def __unicode__(self):
        return unicode(self.mark)

forms.py

class SpecForm(ModelForm):
    class Meta:
        model=Spec
        fields = ('mark','model','engine')  

template

<div class="subnav">
    <div class="row-fluid">
    <form action="" method="get">
        <div class="span3">
            {{ form.mark }}
        </div>
        <div class="span3">
            {{ form.model }}
        </div>
        <div class="span3">
            {{ form.engine }}
        </div>
        <div class="span1"><input class="btn btn-success" type="submit" value="Pobierz" /></div>
    </form>
    </div>
</div>
Cœur
  • 37,241
  • 25
  • 195
  • 267
pejot
  • 5
  • 1
  • 6

1 Answers1

0

because "engine" is not an object and just a string... try...

all_datap = Spec.objects.filter(mark__id=mark, model=model, engine__engine=engine).distinct()

should do the trick.

teewuane
  • 5,524
  • 5
  • 37
  • 43
  • Now error: Join on field 'engine' not permitted. Did you misspell 'engine' for the lookup type? – pejot May 16 '13 at 12:03