将变量传递给Form类

时间:2016-04-14 19:25:45

标签: python django

我使用django-mptt-autocomplete,用于树视图模板中的hierarhy。

fosmotr_techobject = TechObject.objects.order_by('tree_id', 'lft')
    class lookupForm(forms.ModelForm):
        class Meta:
            model = Osmotr
            fields = ('Osmotr_ostanovka', 'Osmotr_techobject', )        
            exclude = ('Osmotr_ostanovka', )
            widgets = {
                'Osmotr_techobject': FancyTreeWidget(queryset=fosmotr_techobject,model=Osmotr),
                'Osmotr_ostanovka': forms.HiddenInput()     
            }

但是我需要在小部件中过滤查询集,这就是问题。

如果我尝试在Form类中设置queryset - 它不起作用

NameError: name 'fosmotr_techobject' is not defined

当我尝试从函数获取queryset时 - 它也不起作用

def get_zavod(route_id):
    zzavod = Marshrut.objects.filter(id=route_id).values_list('marshrut_Zavod', flat=True)
    fosmotr_techobject = TechObject.objects.filter(TechObject_zavod__in = zzavod).order_by('tree_id', 'lft')
    return fosmotr_techobject       

class lookupForm(forms.ModelForm):
    class Meta:
        model = Osmotr
        fields = ('Osmotr_ostanovka', 'Osmotr_techobject', )        
        exclude = ('Osmotr_ostanovka', )
        widgets = {
            'Osmotr_techobject': FancyTreeWidget(queryset=get_zavod(route),model=Osmotr),
            'Osmotr_ostanovka': forms.HiddenInput()     }
    def __init__(self, *args, **kwargs):
        route = kwargs.pop('route', None)

我从路线

传递路线到 init
def lookup_add(request, route_id, stop_id):
    if not request.user.is_authenticated():
        return redirect('/routes/login/')
    else:
        if request.method == "POST":
            form = lookupForm(request.POST)
            if form.is_valid():
                lookup = form.save(commit=False)
                lookup.Osmotr_ostanovka_id = stop_id
                lookup.save()           
                return redirect('/routes/stops/get/' + stop_id + '/') 
        else:
            form = lookupForm(route = route_id)
        return render(request, 'lookup_add.html', {'form': form, 'username': auth.get_user(request)})

同样的错误

  NameError: name 'route' is not defined

我的模特:

class Zavod(models.Model):
    class Meta:
        db_table = "zavod"
    zavod_kod = models.CharField(max_length = 4, primary_key=True)
    zavod_text = models.CharField(max_length = 59)
    def __unicode__(self):
        return self.zavod_text

class Marshrut(models.Model):
    class Meta:
        db_table = "marshrut"
    marshrut_text = models.CharField(max_length = 59)
    marshrut_Zavod = models.ForeignKey(Zavod, null=True)
    def __unicode__(self):
        return self.marshrut_text

class TechObject(MPTTModel):
    class Meta:
        db_table = "techObject"
    class MPTTMeta:
        order_insertion_by = ['TechObject_text']    
    TechObject_SAPid = models.CharField(max_length = 59, primary_key=True)
    TechObject_zavod = models.ForeignKey(Zavod, null=True)
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True)
    def __unicode__(self):
         return self.TechObject_text

 class Osmotr(models.Model):
    class Meta:
        db_table = "osmotr"
    Osmotr_ostanovka = models.ForeignKey(Ostanovka, null=True)
    Osmotr_techobject = models.ForeignKey(TechObject, null=True)
    def __unicode__(self):
        return str(self.id)

我也尝试过使用全局变量,但没有结果。

0 个答案:

没有答案