Django form with dynamic queryset ModelMultipleChoiceField - python

I'm trying to pass a queryset to a forms ModelMultipleChoiceField as an initial value. I want to send a filtered queryset as all the choices and an initial selection. It seems to fail is_valid. Can anyone tell me what I'm doing wrong?
forms.py
class sendListForm(forms.Form):
recipients = forms.ModelMultipleChoiceField(queryset = CustomUser.objects.all())
title = forms.CharField(max_length=100,required=True)
description = forms.CharField(max_length=500,required=False,widget=forms.Textarea(attrs={'cols': 20, 'rows': 4}))
extraInfo = forms.CharField(max_length=500,required=False, help_text='Add a message to send',widget=forms.Textarea(attrs={"rows":4, "cols":20}),label='Extra Message')
startDate = forms.DateField(required=False,widget=forms.HiddenInput())
startTime = forms.TimeField(required=False,widget=forms.HiddenInput())
endDate = forms.DateField(required=False,widget=forms.HiddenInput())
endTime = forms.TimeField(required=False,widget=forms.HiddenInput())
yearName = forms.CharField(widget=forms.HiddenInput())
def __init__(self, *args, **kwargs):
recipients = kwargs.pop('recipients')
super(sendListForm, self).__init__(*args, **kwargs)
self.fields['recipients'] = forms.ModelMultipleChoiceField(queryset=recipients)
views.py
def eventSendList(request, modelPk=None):
event = get_object_or_404(Event, pk=modelPk)
if request.method == 'POST':
form = sendListForm(request.POST,recipients=CustomUser.objects.all())
if form.is_valid():
print('valid')
baseInfo = {
'recipients':recipients,
'title':event.title,
'description':event.description,
'startDate':event.startDate,
'startTime':event.startTime,
'endDate':event.endDate,
'endTime':event.endTime,
'yearName':event.yearName.name,
}
classParents = CustomUser.objects.all()
form = sendListForm(initial=baseInfo,recipients=classParents)
return render(request, 'page/sendListForm.html',{'form':form})
It never gets past the if form.is_valid():... in the view.

you can easily find your form errors by printing the form.errors field.

Related

Unable to Update InlineFormset in Django with CBV

class PreChildrenView(CreateView):
model = PreDealDetails2
template_name = 'cam_app/children_form.html'
fields = '__all__'
success_url = reverse_lazy('forms_app:deal-entering')
session_initial = 'children_'
def get_initial(self,**kwargs):
initial = super(PreChildrenView, self).get_initial(**kwargs)
initial['deal_id'] = self.request.session['deal_id']
return initial
def get_context_data(self, **kwargs):
data = super(PreChildrenView, self).get_context_data(**kwargs)
if self.request.POST:
data['childrens'] = ChildrenFormSet(self.request.POST)
print('post')
else:
print('get')
data['childrens'] = ChildrenFormSet()
data['childrens'].extra = 5
data['info'] = 'Children Details'
return data
def form_valid(self, form):
print('wwwww')
context = self.get_context_data()
childrens = context['childrens']
if form.is_valid():
pass
if childrens.is_valid():
count = 0
self.object = form.save()
childrens.instance = self.object
childrens.save()
self.request.session[self.session_initial + 'children_count'] = count
self.request.session['valid_children'] = True
messages.success(self.request, 'Successfully filled Children Details')
return self.render_to_response(self.get_context_data(form=form))
else:
return super(PreChildrenView, self).form_invalid(form)
class UpdatePreChildrenView(UpdateView):
model = PreDealDetails2
template_name = 'cam_app/children_form.html'
fields = '__all__'
success_url = reverse_lazy('forms_app:deal-entering')
session_initial = 'children_'
def get_object(self, queryset=None):
return PreDealDetails2(deal_id = self.request.session['deal_id'])
def get_context_data(self, **kwargs):
data = super(UpdatePreChildrenView, self).get_context_data(**kwargs)
if self.request.POST:
a = PreDealDetails2.objects.get(deal_id = self.request.session['deal_id'])
data['childrens'] = ChildrenFormSet(self.request.POST)
print('post')
else:
print('get')
data['childrens'] = ChildrenFormSet(instance=self.object)
data['childrens'].extra = 5
data['info'] = 'Children Details'
return data
def form_valid(self, form):
print('update valid')
context = self.get_context_data()
childrens = context['childrens']
if form.is_valid():
print('wejri')
self.object =form.save()
if childrens.is_valid():
childrens.instance = self.object
childrens.save()
count = 0
self.request.session[self.session_initial + 'children_count'] = count
self.request.session['valid_children'] = True
messages.success(self.request, 'Successfully filled Children Details')
return self.render_to_response(self.get_context_data(form=form))
else:
return super(UpdatePreChildrenView, self).form_invalid(form)
else:
print('sfeief')
class PreChildrenRedirectView(RedirectView):
def get_redirect_url(self):
flag = 0
try:
PreDealDetails2.objects.get(deal_id=self.request.session['deal_id'])
flag = 1
except:
pass
if flag == 1:
return reverse("cam_app:update-prechildren-view")
else:
return reverse("cam_app:create-prechildren-view")
I am using above code to enter details to form and later update form.
CreateView is working fine and saving data.
UpdateView is displaying data. When i try to save updateview it is showing following error :-
Code Flow :-
First PreChildrenRedirectView is called. It checks if data already exist or not. If it does then Updateview is called and if it doesnot exist then Createview is called.
I am not using slug. Instead i am using get_object in Updateview. Let me know what is causing problem or how to solve the error.
Update
I have changed the forms.py to remove the error in child. But Deal Id is still showing error. I am also attaching the forms.py below
class ChildrenForm(forms.ModelForm):
class Meta:
model = PreChildrenDetails
fields = '__all__'
def __init__(self, *args, **kwargs):
super(ChildrenForm, self).__init__(*args, **kwargs)
self.fields['deal_id'].widget.attrs['readonly'] = True
self.helper = FormHelper(self)
self.helper.form_show_labels = False
def clean(self):
print('wow')
pass
ChildrenFormSet = inlineformset_factory(PreDealDetails2,PreChildrenDetails, form=ChildrenForm, fields = '__all__', extra=1)
Below is screenshot of error after updating

How can I pass current logged in user while form submission in django?

I am trying to create a form that allows current logged in user to submit data.
The form comprises of the field - amount, rate(interest), timestamp(automatically picked up), and currently logged in user.
Data is not passing into the database and giving an error like - The view investors.views.InvestView didn't return an HttpResponse object. It returned None instead.
views.py
def InvestView(request):
if request.method == 'GET':
investment_form = InvestorsForm(request.user)
context = {'investment_form': investment_form}
return render(request, 'investors/form.html', context)
if request.method == 'POST':
investment_form = InvestorsForm(request.POST or None, instance=request.user)
if investment_form.is_valid():
amount = investment_form.cleaned_data['amount']
interest = investment_form.cleaned_data['rate']
saving = investment_form.save(commit=False)
# Passing Logged in user
investor = request.user
print(investor)
saving.investor = request.user.id
saving.save()
messages.success(request, f'New Investment Done!')
return redirect('/myinvest/')
forms.py
class InvestorsForm(forms.ModelForm):
class Meta :
model = Investment
fields = ['amount', 'rate']
def __init__(self, user, *args, **kwargs):
self.user = user
super(InvestorsForm, self).__init__(*args, **kwargs)
models.py
class Investor(models.Model):
name = models.CharField(max_length=99)
user = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.name
class Investment(models.Model):
amount = models.FloatField(blank=False)
rate = models.FloatField(blank=False)
timestamp = models.DateField(default=datetime.now)
investor = models.ForeignKey(Investor, on_delete=models.CASCADE)
def __str__(self):
return str(self.investor)
Data must be stored into the database and redirect the page to myinvest section simultaneously.
This issue has nothing to do with adding the user. It is because you do nothing in case the validation fails.
You should turn your function the other way round so that the render is hit in all cases.
def InvestView(request):
if request.method == 'POST':
investment_form = InvestorsForm(request.POST)
if investment_form.is_valid():
saving = investment_form.save(commit=False)
saving.investor.user = request.user
saving.save()
messages.success(request, f'New Investment Done!')
return redirect('/myinvest/')
else:
investment_form = InvestorsForm()
context = {'investment_form': investment_form}
return render(request, 'investors/form.html', context)
Note the indentation.
The actual failure to validate is because you have changed the signature of the form to accept a user parameter first, instead of the expected data. However it is not clear why you have done this as you do not use that value, but instead set the user in the view. You should remove that __init__ method.
Another error: your form's __init__ method takes a user as its first argument:
def __init__(self, user, *args, **kwargs):
In the case of a GET request, you pass it correctly:
investment_form = InvestorsForm(request.user)
But then with a POST request, you forget it:
investment_form = InvestorsForm(request.POST or None, instance=request.user)
Passing request.user as the first argument there as well should help.
You need to replace saving.investor = request.user.id with saving.investor.user = request.user.

Django2 How can i make some conditions in my Class based View

please i have an issue , i want to check the difference between two dates from my form, then allows to create the object using my class based_View if the date is bigger than an other attribute, if not render it to an other page without inserting nothing in the database.here is my
# view
class AddVacation(LoginRequiredMixin, CreateView):
form_class = VacationCreateForm
template_name = 'vacation.html'
login_url = 'login'
def form_valid(self, form):
instance = form.save(commit=False)
instance.employee = self.request.user
return super(AddVacation, self).form_valid(form)
# form:
class VacationCreateForm(forms.ModelForm):
class Meta:
model = VacationModel
fields = [
'type',
'startDate',
'enddate',
]
class VacationCreateForm(forms.ModelForm):
class Meta:
model = VacationModel
fields = [
'type',
'startDate',
'enddate',
]
def clean(self):
start_date = self.cleaned_data['start_date']
end_date = self.cleaned_data['end_date']
from dateutils import parse
start_date = parse(start_date)
end_date = parse(end_date)
# make your calculations
raise forms.ValidationError("message.")
class AddVacation(LoginRequiredMixin, CreateView):
form_class = VacationCreateForm
template_name = 'vavcation.html'
login_url = 'login'
def post(self, request, *args, **kwargs):
"""
Handles POST requests, instantiating a form instance with the passed
POST variables and then checked for validity.
"""
form = self.get_form()
if form.is_valid():
return self.form_valid(form)
else:
redirect(to_ur_another_view)
I'm not solving your specific problem I just deliver the idea or what can you do to jump over your problem
I found this solution to check the delay ! i was thinking class basedView are always the best
def create_vacation(request):
form = VacationCreateForm(request.POST or None)
error_message = ""
if form.is_valid():
dateD = request.POST.get("Startdate")
dateF = request.POST.get("Enddate")
fmt = '%Y-%m-%d'
dS = datetime.strptime(str(dateS), fmt)
dE = datetime.strptime(str(dateE), fmt)
dateDiff = (dE - dS).days
q = Employee.objects.get(id__iexact=request.user.id)
if dateDiff < q.VacationDays:
instance = form.save(commit=False)
instance.employee = request.user
instance.save()
else:
error_message = "Impossible check Your demand"
context = {
"form": form,
"error_message": error_message,
}
return render(request, 'vacation.html', context)

form.is_valid() is false why?

I want to store a POST request to a database, so I a had model form MessageForm and called it from views to validate the data and save it.
models.py
class phoneNumber(models.Model):
address = models.CharField(max_length=15)
def __str__(self):
return self.address
class Message(models.Model):
to = models.ForeignKey(phoneNumber, null=True)
sentfrom = models.CharField(max_length=15, null=True)
content = models.TextField(null=True)
def __str__(self):
return '%s' % (self.content)
forms.py
class MessageForm(forms.ModelForm):
class Meta:
model = Message
fields = '__all__'
def __init__(self, *args, **kwargs):
to = kwargs.pop('to', '')
super(MessageForm, self).__init__(*args, **kwargs)
self.fields['to']=forms.ModelChoiceField(queryset=phoneNumber.objects.filter(address=to))
views.py
#csrf_exempt
def incoming(request):
if request.method == "POST":
form = MessageForm(request.POST)
if form.is_valid():
twiml = '<Response><Message>Yes</Message></Response>'
else:
twiml = '<Response><Message>No</Message></Response>'
else:
twiml = '<Response><Message></Message></Response>'
return HttpResponse(twiml, content_type='text/xml')
Nothing is saved and I get No response when I test it.
You can access your errors in views.py
#csrf_exempt
def incoming(request):
if request.method == "POST":
form = MessageForm(request.POST)
if form.is_valid():
twiml = '<Response><Message>Yes</Message></Response>'
else:
print(form.errors)
print(form.non_field_errors)
twiml = '<Response><Message>No</Message></Response>'
else:
twiml = '<Response><Message></Message></Response>'
return HttpResponse(twiml, content_type='text/xml')
Have you tried:
def __init__(self, *args, **kwargs):
to = kwargs.pop('to', '')
super(MessageForm, self).__init__(*args, **kwargs)
self.fields['to'].queryset = phoneNumber.objects.filter(address=to)
Also I'm not sure if you want to pass anything to the form in views method, because right now your to in form is empty string, so your queryset is querying on phoneNumber.objects.filter(address=''), which may or may not be what you want.
Edit:
The reason that to is empty string because kwargs.pop('to', '') means "pop argument to out from kwargs, if to is not there the default is ''". In your views you do:
form = MessageForm(request.POST)
but you didn't feed the constructor with any to argument, so kwargs.pop('to', '') would get '' as default value. You might need something like:
form = MessageForm(request.POST, to="white house")

Django form not inserting form values

I have a from created which is generated by a model. I can save the form, but the form data is not inserted into the table. The insert occurs, but with blank data. Any help would be greatly appreciated.
models.py
class HelpDefinition(models.Model):
org = models.IntegerField(default=0)
help_type = models.CharField(max_length=255)
help_content = models.TextField(blank=True)
def __unicode__(self):
return self.name
views.py
def index(request, org_id=None):
help_def = HelpDefinition()
if org_id:
help_def = HelpDefinition.objects.get(org=org_id)
if request.method == 'POST':
form = FormHelp(request.POST)
if form.is_valid():
help_def.save()
messages.success(request, 'Saved!')
else:
messages.error(request, 'Ugh')
else:
form = FormHelp(request=request, initial=initial_data)
return {
'form': form,
}
forms.py
class FormHelp(forms.Form):
org = forms.CharField(widget=forms.HiddenInput, required=True)
help_type = forms.ChoiceField(abel='Text', required=True)
help_content = forms.CharField(label='Description', required=True, widget=forms.Textarea)
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
super(FormHelp, self).__init__(*args, **kwargs)
Due that you dont use ModelForm, you need to set your attributes one by one.
if request.method == 'POST':
form = FormHelp(request.POST)
if form.is_valid():
help_def.org = form.cleaned_data.get("org")
help_def.help_type = form.cleaned_data.get("help_type")
help_def.help_type = form.cleaned_data.get("help_content")
help_def.save()

Categories