0

我有问题,因为我正在使用智能选择(3 个选择字段)来过滤我的表格。但是引擎 id 是错误的,并且没有得到正确的查询。我不需要从 db 获取 id,而是获取常用名称。

视图.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})

mark = form.cleaned_data['mark']

我得到像“1”这样的id,但需要像“porshe”这样的名字

模型.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)

表格.py

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

模板

<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>
4

1 回答 1

0

因为“引擎”不是一个对象,只是一个字符串......试试......

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

应该做的伎俩。

于 2013-05-15T22:48:35.383 回答