创建动态选择字段 [英] Creating a dynamic choice field

查看:91
本文介绍了创建动态选择字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在django中,如何创建一个动态选择字段,我遇到了麻烦。我有一个模型设置如下:

  class rider(models.Model):
user = models.ForeignKey (User)
waypoint = models.ManyToManyField(Waypoint)

class Waypoint(models.Model):
lat = models.FloatField()
lng = models。 FloatField()

我想要做的是创建一个选项字段whos值是相关的路点这个车手(这是登录的人)。



目前,我覆盖了我的表单中的init,如下所示:



pre> class waypointForm(forms.Form):
def __init __(self,* args,** kwargs):
super(joinTripForm,self) __init __(* args,** kwargs)
self.fields ['waypoints'] = forms.ChoiceField(choices = [(o.id,str(o))for o在Waypoint.objects.all()] )

但是所有这一切都列出所有的路标点,他们没有与任何特定的骑手相关联。有任何想法吗?谢谢。

解决方案

您可以通过将用户传递给表单init来过滤路点。



pre> class waypointForm(forms.Form):
def __init __(self,user,* args,** kwargs):
super(waypointForm,self )___(* args,** kwargs)
self.fields ['waypoints'] =在Waypoint中为o的form.ChoiceField(
choices = [(o.id,str(o))。 objects.filter(user = user)]

从您的视图启动表单传递用户

  form = waypointForm(user)

在模型窗体的情况下

  class waypointForm(forms.ModelForm):
def __init __(self,user,* args,** kwargs):
super(waypointForm,self).__ init __(* args,** kwargs)
self.fields ['waypoints'] = forms.ModelChoiceField(
queryset = Waypoint.objects.filter(user = user)


Meta类:
model = Waypoint


I'm having some trouble trying to understand how to create a dynamic choice field in django. I have a model set up something like:

class rider(models.Model):
     user = models.ForeignKey(User)
     waypoint = models.ManyToManyField(Waypoint)

class Waypoint(models.Model):
     lat = models.FloatField()
     lng = models.FloatField()

What I'm trying to do is create a choice Field whos values are the waypoints associated with that rider (which would be the person logged in).

Currently I'm overriding init in my forms like so:

class waypointForm(forms.Form):
     def __init__(self, *args, **kwargs):
          super(joinTripForm, self).__init__(*args, **kwargs)
          self.fields['waypoints'] = forms.ChoiceField(choices=[ (o.id, str(o)) for o in Waypoint.objects.all()])

But all that does is list all the waypoints, they're not associated with any particular rider. Any ideas? Thanks.

解决方案

you can filter the waypoints by passing the user to the form init

class waypointForm(forms.Form):
    def __init__(self, user, *args, **kwargs):
        super(waypointForm, self).__init__(*args, **kwargs)
        self.fields['waypoints'] = forms.ChoiceField(
            choices=[(o.id, str(o)) for o in Waypoint.objects.filter(user=user)]
        )

from your view while initiating the form pass the user

form = waypointForm(user)

in case of model form

class waypointForm(forms.ModelForm):
    def __init__(self, user, *args, **kwargs):
        super(waypointForm, self).__init__(*args, **kwargs)
        self.fields['waypoints'] = forms.ModelChoiceField(
            queryset=Waypoint.objects.filter(user=user)
        )

    class Meta:
        model = Waypoint

这篇关于创建动态选择字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆