开发者

django forms queryset doudt

开发者 https://www.devze.com 2023-02-08 08:06 出处:网络
when were i fetch a data from model itdisplays the all data from table. support : i have table xyz class xyzform(forms.Form):

when were i fetch a data from model it displays the all data from table.

support : i have table xyz

class xyzform(forms.Form):
   test=xyz.objects.filter(m='name').vlaues('doj')

in html

{{form}}

it won't output nothing,only submit button is displayed

if i remove vlaues('doj'),it displays all the data .

1.My question is ,how to display only specific filed in the form ?

2.Another question how to fetch remote.meta.get('remote_user') in to query set filter in the form ?

for example :

 开发者_运维百科 name =remote.meta.get('remote_user') 
  test=xyz.objects.filter(m='name')

Please reply with few examples that will be easy for to understand...


You need to read the django forms docs from the beginning to understand how to use the forms framework.
http://docs.djangoproject.com/en/dev/topics/forms/

Following the examples will illustrate "how it works" (5 minutes).

Forms look more like this:

class XYZForm(forms.Form):
    field = forms.CharField() 
    # forms only know how to display themselves with relevant 
    # forms.FOOField definitions.

As for passing request variables into the form, you'd have to override __init__ so you can pass extra arguments into the form constructor from your view.

class XYZForm(forms.Form):
    field = forms.CharField()

    def __init__(self, remote_user, *args, **kwargs):
        super(XYZForm, self).__init__(*args, **kwargs)
        # do something with remote_user

# view
form = XYZForm(remote_user = remote.meta.get('remote_user'))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号