Adding Values To Complex Django View
I am trying to get a dictionary of values from the Photographer model to appear in my form. Afterwards, the user will select a Photographer, submit the form (along with other infor
Solution 1:
I think you are missing a fundamental aspect of Django programming, the Django form. You should create a form like:
class AForm(forms.Form):
model = forms.ModelChoiceField(queryset=Model.objects.all())
photographer = forms.ModelChoiceField(queryset=Photographer.objects.all())
class Meta:
model = A
fields = ('model', 'photographer', )
Then your create view would look something like:
@login_requireddefcreate_in(request, slug):
if request.method == 'POST':
form = AForm(request.POST)
if form.is_valid():
instance = form.save(commit=False)
instance.user = request.user
return HttpResponseRedirect("/somewhere/")
else:
form = AForm()
return render_to_response('create_in.html', { 'form': form }, context_instance=context)
It's hard to figure out what you are trying to do, I think you are getting overly tricky as I've never needed to extend BaseForm and I've done lots of forms with special filters on the selection. Usually these are done something like:
classAForm(forms.Form):
def__init__(self, profile, *args, **kwargs):
super(AForm, self).__init__(*args, **kwargs)
self.fields['photographer'].queryset = models.Photographer.objects.filter(profilephotographer__profile=profile)
Post a Comment for "Adding Values To Complex Django View"