Filter queryset inside a form - python

I have one app that holds a list of work orders, and another app that holds list of parts.
class Order(models.Model):
parts = models.ManyToManyField(Part, blank=True) # Assosiated parts
class Part(models.Model):
partnum = models.CharField(max_length=20) # Part number
mwos = models.ManyToManyField('mtn.Order', blank=True) # Assosiated work orders
Now i want to add a button to my DetailView for order which will open a list of parts, which i will be able to add to my order. At the moment i have a created an UpdateView for my order
class AddPartView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
model = Order
form_class = AddPartForm
...
and a form
class AddPartForm(forms.ModelForm):
class Meta:
model = Order
fields = ['parts', ]
labels = {'parts': "Parts", }
def FilterList(request):
qs = Part.objects.all()
search_part_query = request.GET.get('search_part')
if is_valid_queryparam(search_part_query):
qs = qs.filter(Q(partnum__icontains=search_part_query)
| Q(descr__icontains=search_part_query)
).distinct()
return qs
def __init__(self, *args, **kwargs):
super(AddPartForm, self).__init__(*args, **kwargs)
self.fields["parts"].widget = CheckboxSelectMultiple()
self.fields["parts"].queryset = self.FilterList()
for this template
{% block content %}
<form method="GET" action=".">
<div class="form-row justify-content-start">
<div class="form-group col-md align-self-center">
<div class="input-group">
<input class="form-conrol py-2 border-right-0 border" type="search" placeholder="Find part" name="search_part">
<span class="input-group-append">
<div class="input-group-text bg-transparent">
<i class="fa fa-search"></i>
</div>
</span>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary btn-sm btn-block">Search</button>
</form>
<form action="{% url 'mtn:add_part' order.id %}" method='post'>
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Save</button>
</form>
{% endblock content %}
But when i'm executing it i get
'AddPartForm' object has no attribute 'GET'
error.
I am new to programming, so maybe i am approaching this the wrong way.

A form is normally not aware of the request object. You can make such a form, for example with:
class AddPartForm(forms.ModelForm):
class Meta:
model = Order
fields = ['parts', ]
labels = {'parts': "Parts", }
widgets = {
'parts': CheckboxSelectMultiple
}
def filter_list(self, request):
qs = Part.objects.all()
search_part_query = request.GET.get('search_part')
if is_valid_queryparam(search_part_query):
qs = qs.filter(Q(partnum__icontains=search_part_query)
| Q(descr__icontains=search_part_query)
).distinct()
return qs
def __init__(self, *args, request=None, **kwargs):
super(AddPartForm, self).__init__(*args, **kwargs)
self.fields["parts"].queryset = self.filter_list(request)
In the AddPartView, you can the pass the request as parameter to the form by overriding the .get_form_kwargs(..) method [Django-doc]
class AddPartView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
model = Order
form_class = AddPartForm
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs.update(request=self.request)
return kwargs

Related

Django: How to detect the focus out in django template and call a function on it

I am working on a project. Need help in template focus out events on Django.
model.py
class Route(models.Model):
route_no = models.SmallIntegerField(default=0)
xname = models.CharField(max_length=40)
class Booth(models.Model):
booth_no = models.SmallIntegerField(default=0)
route_no = models.ForeignKey(Route,
on_delete=models.CASCADE,
db_column='route_no')
View.py
class BoothCreateListView(CreateView, ListView):
model = models.Booth
form_class = booth.BoothForm
template_name = 'booth/booth_create_list.html'
context_object_name = 'booth_list'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
js_data = list(models.Route.objects.all().values_list('route_no', 'xname'))
context['js_data'] = json.dumps(list(js_data), cls=DjangoJSONEncoder)
return context
template/booth_create_list.html
<div class="col-sm-12 col-md-5">
<form method="post">
{% csrf_token %}
<table class="table table-borderless">
{{ form.as_table }}
</table>
<input class="btn btn-success" type="submit" value="Save">
</form>
{{ form.route.value }}
</div>
<div id='route_no'></div>
<script>
var route_no = document.getElementById('route_no')
var myfunction = function (){
console.log('changing');
route.innerHTML = '{{ check_route_no form.route.value }}'
}
</script>
form/booth.py
class BoothForm(ModelForm):
class Meta:
fields = [
'route_no', 'booth_no',
]
model = models.Booth
widgets = {
'route_no': forms.TextInput(),
}
labels = {
'route_no': 'Route No.',
'booth_no': 'Booth No',
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['route_no'].widget.attrs.update(
{'onfocusout':'myfunction()'}
)
for name, field in self.fields.items():
field.widget.attrs.update(
{'class': 'form-control form-control-user'}
)
templatetags/booth_template_tags.py
from booth import models
register = template.Library()
#register.simple_tag
def check_route_no(route_no):
print(route_no)
route = models.Route.objects.filter(route_no=route_no)
if route.count() == 1:
return route.xname
else:
return "not present"
I want to check the route as user types it in the form for the booth. If route_no is present then show the route xname else not present.
My value passed to the template tag is always none. I am not able to pass the textbox value to the template tag to search in DB.
Please help to check runtime if the route no is there in DB as the user type.

Page not redirecting after form submit

I have a form which creates a new category. Previously, the form was in a different template which worked fine but since it's only a simple form I have decided to render it in modal form instead of redirecting to a different page.
The user can add a new category, however the success message and the page rendering after the form submit is not shown. It only shows up if you refresh the page. The response message is 302.
I've done similar method with other forms which worked perfectly fine.
forms.py
class CategoryModelForm(forms.ModelForm):
def clean_name(self):
print(self.cleaned_data['name'])
name = self.cleaned_data['name']
try:
Category.objects.get(name__iexact=name)
except ObjectDoesNotExist:
return name
raise forms.ValidationError('Category Name already exists.')
class Meta:
model = Category
fields = ['name']
views.py
#method_decorator(login_required, name='dispatch')
class CategoryView(TemplateView):
template_name = 'content/category_list.html'
def get_context_data(self, **kwargs):
context = super(CategoryView, self).get_context_data(**kwargs)
categories = Category.objects.all()
user = self.request.user
category_list = []
for category in categories:
article_count = category.article_count(user)
include = category.show or user.usertype_is_staff() or user.is_superuser
requested_by = category.requested_by if category.requested_by else ''
cat = {
'reference': category.pk,
'name': category.name,
'show': category.show,
'article_count': article_count,
'has_articles': article_count > 0,
'requested_by': requested_by,
'requested_by_name': requested_by.profile.full_name if requested_by and requested_by.profile.full_name
else '-'
}
include and category_list.append(cat)
context['categories'] = category_list
context['form'] = CategoryModelForm(self.request.POST or None)
return context
def post(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs)
if context['form'].is_valid():
context['form'].save()
messages.success(request, 'Successfully created new category.')
return redirect('content:category')
return super(CategoryView, self).render_to_response(context)
category_list.html
<div id="newCategory" data-id="new-account" class="modal fade bd-example-modal-lg"
tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="syn-breadcrumb">
<div class="syndicator-form-container">
<form class="syndicator-form" action="{% url 'content:category' %}"
method="post">
{% csrf_token %}
<div class="">
<h3 class="lighter-weight">
{% if user.usertype_is_supplier %}
Request New Category
{% else %}
Add New Category
{% endif %}
</h3>
</div>
<div class="form-fields">
<div class="non-field-errors">
{{ form.non_field_errors }}
</div>
<div id="{{ form.name.name }}" class="d-flex flex-column fields">
<div class="lighter-weight"><label for="id_name">Name</label></div>
<div>{{ form.name }}</div>
<div class="field-errors">{{ form.name.errors }}</div>
</div>
</div>
<div class="submit-button">
<button type="submit" class="btn btn-primary form-control">{% trans 'Submit' %}</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
I want the page to be redirected to an updated list of the categories after the form submit with the success message. As well as show the error message if the category name already exists or if the fields re empty.
You are using self.request instead of request even though request is already passed in post method
def post(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs)
context['form'] = CategoryModelForm(request.POST or None) # use request.POST
I would rather suggest you to use a FormView or rather a generic view. (docs). You won't need to handle the form by yourself.
#method_decorator(login_required, name='dispatch')
class CategoryView(FormView):
template_name = 'content/category_list.html'
form = CategoryModelForm
success_url = reverse("content:category")
def form_valid(self, form):
self.obj = form.save(commit=True)
messages.success(self.request, 'Successfully created new category.')
return super(CategoryView, self).form_valid(form)
def get_context_data(self, **kwargs):
context = super(CategoryView, self).get_context_data(**kwargs)
categories = Category.objects.all()
user = self.request.user
category_list = []
for category in categories:
article_count = category.article_count(user)
include = category.show or user.usertype_is_staff() or user.is_superuser
requested_by = category.requested_by if category.requested_by else ''
cat = {
'reference': category.pk,
'name': category.name,
'show': category.show,
'article_count': article_count,
'has_articles': article_count > 0,
'requested_by': requested_by,
'requested_by_name': requested_by.profile.full_name if requested_by and requested_by.profile.full_name
else '-'
}
include and category_list.append(cat)
context['categories'] = category_list
# form will be automatically added to context

Django Form Field Validation failed to work

Here is my form.
class POICreateForm(ModelForm):
def __init__(self, *args, **kwargs):
super(POICreateForm, self).__init__(*args, **kwargs)
self.fields['latitude'].widget = HiddenInput()
self.fields['longitude'].widget = HiddenInput()
self.fields['picture'].required = True
class Meta:
fields = ['name', 'vip', 'category', 'place', 'latitude', 'longitude', 'picture', 'website', 'description',
'phone',
'email']
model = PointOfInterest
def clean(self):
cleaned_data = super(POICreateForm, self).clean()
pic = cleaned_data.get('picture', None)
if not pic:
msg = 'You must Choose an image'
self.add_error('picture', msg)
return cleaned_data
I have given required=True for picture field and also I overridded the clean method. BUt both failed to show the validation error.
Here is my view
class POIFormCreateView(LoginRequiredMixin, CreateView):
login_url = '/login/'
model = PointOfInterest
form_class = POICreateForm
success_url = reverse_lazy('POIs')
def post(self, request, *args, **kwargs):
query_dict = request.POST.dict()
super(POIFormCreateView, self).post(request, *args, **kwargs)
filtered_dict = {i: j for i, j in query_dict.items() if i.endswith('day')}
out = PointOfInterestQuery.create_or_update_open_hours(filtered_dict)
if out:
return HttpResponseRedirect(reverse('POIs'))
return HttpResponse('Error :(')
template.html
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<script type="text/javascript">
</script>
<h1>New Point Of Interest</h1>
<form role="form" method="POST" action="/poi/add/" class="post-form form-horizontal"
enctype="multipart/form-data" name="poi_create" onsubmit="return validateForm()">{% csrf_token %}
<div class="container row">
<div class="col-lg-6">
<!-- customizing form -->
{{ form|crispy }}
<!-- End of customization -->
</div>
<div class="col-lg-6">
<div id="map"></div>
{% include 'open_hours.html' %}
</div>
</div>
<button type="submit" id="poi-submit" class="save btn btn-default btn-primary center-block">Save
</button>
</form>
<div class="modal-footer"><a href="/poi/">
<button type="button" class="btn btn-default btn-danger center-block">Cancel
</button>
</a></div>
{% endblock %}
You've overridden the post method and avoided using the form at all. All of this logic should be in the form_valid method instead; that allows the CreateView to do the normal job of form validation and re-displaying the page with errors if the form is invalid, while your processing happens if the form is valid.
Note also however that you should be getting your values to populate filtered_dict from the form's cleaned_data dict, not directly from request.POST.
class POIFormCreateView(LoginRequiredMixin, CreateView):
login_url = '/login/'
model = PointOfInterest
form_class = POICreateForm
success_url = reverse_lazy('POIs')
def form_valid(self,form):
super(POIFormCreateView, self).form_valid(form)
filtered_dict = {i: j for i, j in self.request.POST.items() if i.endswith('day')}
out = PointOfInterestQuery.create_or_update_open_hours(filtered_dict)
if out:
return HttpResponseRedirect(reverse('POIs'))
return HttpResponse('Error :(')

django model dropdown missing in html view

I'm writing a django app, and I'd like for users to be able to select a [team_number] from a dropdown menu, then when they hit submit be redirected to a page that renders out the database information associated with that selection. I'm using the redirect class View, but the problem I'm having is that there is no dropdown menu showing up to select [team_number] from on the html page team-stats.html.
views.py:
class TeamStatsView(View):
def get(self, request, *args, **kwargs):
return render(request, 'team-stats.html',
{'team_number': TeamStats()})
def post(self, request, *args, **kwargs):
team_number = TeamStats(request.POST, request.FILES)
if team_number.is_valid():
# do stuff & add to database
team_number.save()
team_number = TeamStats.objects.create()
# use my_file.pk or whatever attribute of FileField your id is
# based on
return HttpResponseRedirect('/team-stats/%i/' % team_number.pk)
return render(request, 'team-stats.html', {'team_number': team_number})
models.py:
class Team(models.Model):
team_number = models.IntegerField()
team_notes = models.CharField(max_length=150)
event_id = models.ForeignKey(
'Event', on_delete=models.CASCADE, unique=False)
def __unicode__(self):
return str(self.team_number)
class Meta:
db_table = 'teams'
app_label = 'frcstats'
forms.py:
class TeamStats(forms.ModelForm):
class Meta:
model = Team
fields = ['team_number']
team-stats.html:
<form method="post" action="">
{% csrf_token %} {{ TeamStatsView }}
<input type="submit" value="Submit" />
</form>
If there are any other files that I need to update into here to show what I'm trying to do, please let me know. Thanks
Try changing your view variable name to team_numbers and replacing your team-stats.html snippet with the following:
<form method="post" action="">
<select name="teams">
{% for team_number in team_numbers %}
<option value="{{ team_number }}">Team Num: {{ team_number }}</option>
{% endfor %}
</select>
</form>
Then update your view to:
class TeamStatsView(View):
def get(self, request, *args, **kwargs):
return render(request, 'team-stats.html',
{'team_numbers':Team.objects.values('team_number')})
You can use choices=NUMBERS
NUMBERS = (
('1','1'),
('2','2'),
('3','3'),
('4','4')
)
class Team(models.Model):
team_number = models.IntegerField(choices=NUMBERS )
team_notes = models.CharField(max_length=150)
event_id = models.ForeignKey(
'Event', on_delete=models.CASCADE, unique=False)
def __unicode__(self):
return str(self.team_number)
class Meta:
db_table = 'teams'
app_label = 'frcstats'
Your view variable is called team_number.
Try to change TeamStatsView into team_number:
<form method="post" action="">
{% csrf_token %} {{ team_number }}
<input type="submit" value="Submit" />
</form>

Django: Save foreign key value in a forms raise an error

I have to tables correlated with a foreign key.
Demographics tables with primary key patient_id (required) and diagnosis having foreign key patient. I've created two forms. I save the fields on first form and then the patient field is autocompleted in diagnosis form.
The problem, is when I'm trying to save the fields' values on diagnosis form, I get an error on the first form which says 'This field is required' even this field's value was stored a minute ago.
Edited: This is my solution
input.html (template)
<form class="form-horizontal" method="post">
{% csrf_token %}
<div class="tabbable">
<ul class="nav nav-tabs">
<li class="active">
Demographics
</li>
<li>
Diagnosis
</li>
<li>
A_b_sickle
</li>
</ul>
<div class="tab-content">
<div class="tab-pane fade in active" id="1">
<input type="hidden" name="form_id" value="demographics">
<p>{%crispy frm%}</p>
</div>
<div class="tab-pane fade" id="2">
<!-- form controls to be continued here -->
<input type="hidden" name="form_id" value="diagnosis">
{%crispy frm_d%}
<p>Howdy, I'm in Section 2.</p>
</div>
<div class="tab-pane fade" id="3">
<!-- form controls to be continued here -->
<input type="hidden" name="form_id" value="a_b_s">
{%crispy frm_a_b_s%}
<p>Howdy, I'm in Section 3.</p>
</div>
</div>
</div>
<!--<input type="submit" name='submit' value="Submit">-->
</form>
My view.py contains:
def input(request):
if request.method == 'POST':
my_demographics = DemographicForm(request.POST, prefix="demo")
my_diagnosis = DiagnosisForm(request.POST, prefix='diag')
my_a_b_sickle= A_b_sickle_thalForm(request.POST,prefix='a_b_s')
if my_demographics.is_valid() and my_diagnosis.is_valid() and my_a_b_sickle.is_valid:
my_demographics_object = my_demographics.save()
my_diagnosis_object = my_diagnosis.save(commit=False)
my_diagnosis_object.patient = my_demographics_object
my_diagnosis_object.save()
my_a_b_sickle_object = my_a_b_sickle.save(commit=False)
my_a_b_sickle_object.patient = my_demographics_object
my_a_b_sickle_object.save()
else:
my_demographics = DemographicForm(prefix='demo')
my_diagnosis = DiagnosisForm(prefix='diag')
my_a_b_sickle= A_b_sickle_thalForm(prefix='a_b_s')
return render_to_response('input.html', {'frm':my_demographics, 'frm_d': my_diagnosis, 'frm_a_b_s': my_a_b_sickle}, context)
models.py
class demographic(models.Model):
national_health_care_pat_id = models.IntegerField('National Health Care patient id', null=True,blank=True)
patient_hospital_file_number = models.IntegerField(null=True,blank=True)
patient_id = models.IntegerField(unique= True ,primary_key=True)
def __str__(self):
return str(self.patient_id)
class diagnosis(models.Model):
patient = models.ForeignKey(demographic)
age_of_diagnosis = models.IntegerField(null=True,blank=True)
def __str__(self):
return str(self.patient_id)
forms.py
class DemographicForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(DemographicForm, self).__init__(*args, **kwargs)
self.helper=FormHelper(self)
self.fields['date_of_birth'].widget = widgets.AdminDateWidget()
self.helper.layout = Layout(
'national_health_care_pat_id',
'patient_hospital_file_number',
'patient_id',
FormActions(
Submit('submit', "Save changes"),
Submit('cancel', "Cancel")
),
)
self.helper.form_tag = False
self.helper.form_show_labels = True
class Meta:
model = demographic
exclude = []
class DiagnosisForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(DiagnosisForm, self).__init__(*args, **kwargs)
self.helper=FormHelper(self)
self.helper.layout = Layout(
'patient',
'age_of_diagnosis',
FormActions(
Submit('submit', "Save changes"),
Submit('cancel',"Cancel")
),
)
self.helper.form_tag = False
self.helper.form_show_labels = True
class Meta:
model = diagnosis
exclude = []
Do you have any idea what the problem might be? Thank you in advance!
You need to pass in the actual demographic object instead of just the ID for your DiagnosisForm. Django expects ForeignKey fields to point to an instance of the object they represent. You'll also need to exclude the patient field from your DiagnosisForm. This will prevent Django from trying to validate the field.
# forms.py
class DiagnosisForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
. . .
class Meta:
model = diagnosis
exclude = ['patient']
# views.py
from django.shortcuts import get_object_or_404, render
from your_app.models import Patient
def input(request):
patient = get_object_or_404(Patient, id=5)
my_demographics = DemographicForm(request.POST or None)
my_diagnosis = DiagnosisForm(request.POST or None)
if request.method =='POST':
if my_demographics.is_valid() and my_diagnosis.is_valid():
demographic = my_demographics.save()
my_diagnosis.save(commit=False)
my_diagnosis.patient = demographic
my_diagnosis.save()
return render(request, 'input.html', {'frm':my_demographics, 'frm_d': my_diagnosis})

Categories

Resources