django - dynamic select fields in forms

好久不见. 提交于 2019-12-10 18:30:42

问题


I have a model called picks that allows users to select an nfl team (from a static list). Whenever they choose a team, they can no longer choose that team again, so there selection choices are reduced by any teams they have chosen.
I have a function that I call in my view that figures out what teams they have not selected yet, but I cannot figure out how to pass this information to the form. I've looked at many form examples online but have not really found one that shows how to accomplish this. From what I've read, it seems like I should be calling this function from the forms.py file, but the function needs the user id and league id passed in as parameters so I'd rather just do it in the model. Can someone explain, or point me to an explanation showing how to pass in select field choices from a model to a form?

I'm relatively new in both Django and Python so please excuse any obvious oversights I'm making here.

Thanks!

EDIT

Ok, I think perhaps I gave too much non-crucial information for the issue here, I'll try to simplify the question here:

I have a view which creates a form. The form has a select box in it. I calculate the options that should go in the select box inside of the view, based on the current user and a variable in the URL that launched the view. I would like to know how to pass the select options into the form, or if that is not how this should be accomplished.

I don't have the code in front of me at this time to post, but I really don't think it's relevant to the problem. I hope I have made it generic enough.


回答1:


I think that the easiest way to do would be to modify your form's query set. The idea is to exclude the teams that you don't want (since they have been selected already).

Assuming that you have a relationship between Team and User, do the following (in your view)

form.fields['team'].queryset = Teams.objects.exclude(selected_by_user = user.id)

remember one thing :

Teams.objects.exclude(selected_by_user = user.id, date = datetime(2008, 12, 31))

will match the Teams that are not selected by users AND are not from 31/12/2008, while

Teams.objects.exclude(selected_by_user = user.id,).exclude(date = datetime(2008, 12, 31))

will match the Teams that are not picked up by the user OR from date 31/12/2008.

References : Django Querysets (exclude filter)




回答2:


You can first define your form without the dynamic field. AFTER creating an instance of it in your view, you can add the dynamic select field by typing

form_instance.fields['your_field'] = ChoiceField(choices=list_of_two_tuples)

or make use of the ModelChoiceField and type something like

form_instance.fields['your_field'] = ModelChoiceField(queryset=Teams.filter(...))

Or you can use the similar, but probably cleaner mechanism described here.



来源:https://stackoverflow.com/questions/6120118/django-dynamic-select-fields-in-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!