django-datatables-view load filtered model - python

Here we have the basic code for getting django-datatables-view to display
from django_datatables_view.base_datatable_view import BaseDatatableView
class OrderListJson(BaseDatatableView):
# The model we're going to show
model = MyModel
# define the columns that will be returned
columns = ['number', 'user', 'state', 'created', 'modified']
# define column names that will be used in sorting
# order is important and should be same as order of columns
# displayed by datatables. For non sortable columns use empty
# value like ''
order_columns = ['number', 'user', 'state', '', '']
# set max limit of records returned, this is used to protect our site if someone tries to attack our site
# and make it return huge amount of data
max_display_length = 500
def render_column(self, row, column):
# We want to render user as a custom column
if column == 'user':
return '{0} {1}'.format(row.customer_firstname, row.customer_lastname)
else:
return super(OrderListJson, self).render_column(row, column)
def filter_queryset(self, qs):
# use parameters passed in GET request to filter queryset
# simple example:
search = self.request.GET.get(u'search[value]', None)
if search:
qs = qs.filter(name__istartswith=search)
# more advanced example using extra parameters
filter_customer = self.request.GET.get(u'customer', None)
if filter_customer:
customer_parts = filter_customer.split(' ')
qs_params = None
for part in customer_parts:
q = Q(customer_firstname__istartswith=part)|Q(customer_lastname__istartswith=part)
qs_params = qs_params | q if qs_params else q
qs = qs.filter(qs_params)
return qs
This code works fine, however, how do I get it to not display the whole model, but only filtered content from the model? I've tried setting it to model = MyModel.objects.filter(name="example") but this returns an error.

def get_initial_queryset(self):
return MyModel.objects.filter(name="example")
Add this in your Class OrderListJson

You can use the get_initial_queryset method that returns the queryset used to populate the datatable.

Related

Filter multiple fields using single input

My backend in Python :
def resview(request, *args, **kwargs):
if 'uid' not in kwargs:
kwargs = kwargs.copy()
kwargs['filter'] = {}
username = request.GET.get('username')
if username:
kwargs['filter']['user__username__contains'] = username
return resource_view(request, UserProfile, get_userprof, put_userprof,
deleter=del_userprof, api=request.api, order_by='user__username', **kwargs)
I can successfully search for the username but I want to be able to search on multiple fields with a single input and I don't know how to do it.
I have once in similar situation used Regular Expression to search based on keywords & then pulling out entire line until newline. Here I'm mentioning my small code.
Step1: Save the content in Text file
str = open('file.txt', 'r').read()
import re
#to print id
m = re.search('(?<=id: )(.*)', str)
print ("id= " , (m.groups()))
#to print username
m = re.search('(?<=username: )(.*)', str)
print (username= " , (m.groups()))
############
#and so on----Replace keywords whatever you need...
#############
Considering you're using django, I would suggest to take look at SearchFilter which supports searching multiple fields at once.
Beside of that, you can filter queryset by hands with custom implementation of Filter:
class F(django_filters.FilterSet):
username = CharFilter(method='my_custom_filter')
class Meta:
model = User
fields = ['username', 'firstname', 'lastname', 'email']
def my_custom_filter(self, queryset, name, value):
return queryset.filter(
Q(username__ilike=value)
| Q(firstname__ilike=value)
| Q(lastname__ilike=value)
| Q(email__ilike=value)
)
You can store the JSON list in a list variable and filter the results.
filterData(searchString){
searchString = searchString.trim();
return this.data.filter(user=> user.username.toLowerCase().indexOf(searchString.toLowerCase()) !== -1 || user.firstname.toLowerCase().indexOf(searchString.toLowerCase()) !== -1 || user.lastname.toLowerCase().indexOf(searchString.toLowerCase()) !== -1);
}
I suggest that you use two different lists to save data. One filtered and the other un filtered. This way you won't have to call the service again to get original data. Filter is always applied on unfiltered data.

Django-datatable-view throwing error after decorating url

I am using django-datable-view for rendering data from django models.
Everything works fine before decorating the url, after i added login_required to the url, it threw weird error.
According to the doc, it states that i can add login_required to the url.
Below is my code
from django_datatables_view.base_datatable_view import BaseDatatableView
class OrderListJson(BaseDatatableView):
# The model we're going to show
model = MyModel
# define the columns that will be returned
columns = ['number', 'user', 'state', 'created', 'modified']
# define column names that will be used in sorting
# order is important and should be same as order of columns
# displayed by datatables. For non sortable columns use empty
# value like ''
order_columns = ['number', 'user', 'state', '', '']
# set max limit of records returned, this is used to protect our site if someone tries to attack our site
# and make it return huge amount of data
max_display_length = 500
def render_column(self, row, column):
# We want to render user as a custom column
if column == 'user':
return '{0} {1}'.format(row.customer_firstname, row.customer_lastname)
else:
return super(OrderListJson, self).render_column(row, column)
def filter_queryset(self, qs):
# use parameters passed in GET request to filter queryset
# simple example:
search = self.request.GET.get(u'search[value]', None)
if search:
qs = qs.filter(name__istartswith=search)
# more advanced example using extra parameters
filter_customer = self.request.GET.get(u'customer', None)
if filter_customer:
customer_parts = filter_customer.split(' ')
qs_params = None
for part in customer_parts:
q = Q(customer_firstname__istartswith=part)|Q(customer_lastname__istartswith=part)
qs_params = qs_params | q if qs_params else q
qs = qs.filter(qs_params)
return qs
url
url(_(r'^users/all/?$'),
login_required(dashboard.v1.views.OrderListJson.as_view()),
name='all_users'),
i keep getting error 500, if i remove the login_required, everything works well. If i can get suggestions on how to decorate the class view, i will be glad since that is what am trying to achieve

Extra session data not stored during SessionWizardView

I've got a Django SessionWizardView in which I want to add extra data for the user to take advantage of during the steps. Essentially I want to build a list, and a dict which stores information about the steps once they are complete.
The first step in the wizard allows a user to add information about themselves and at the end allows the option to add another person's details. If this option is selected another, conditional, form is rendered & I'd like to provide them with the option to use the data entered previously.
So during the process_step() method I'm creating a list, and then a corresponding dictionary of data for each step in the process. Initially I had these as class attributes, but feel they would be better suited in a user's session so I've attempted to add them like so;
def process_step(self, form):
form_data = self.get_form_step_data(form)
current_step = self.storage.current_step or ''
data_dict = self.request.session.get('data_dict', dict())
data_list = self.request.session.get('data_list', list())
if current_step in data_dict:
# Always replace the existing data for a step.
data_dict.pop(current_step)
if not isinstance(form, TermsForm):
entrant_data = dict()
for k, v in form_data.iteritems():
entrant_data[k] = v
for k in entrant_data.iterkeys():
new_key = re.sub('{}-'.format(current_step), u'', k)
entrant_data[new_key] = entrant_data.pop(k)
data_dict[current_step] = entrant_data
done = False
for i, data in enumerate(data_list):
if data[0] == current_step:
data_list[i] = (
current_step, u'{} {}'.format(
entrant_data['first_name'],
entrant_data['last_name']
)
)
done = True
if not done:
data_list.append(
(
current_step, u'{} {}'.format(
entrant_data['first_name'],
entrant_data['last_name']
)
)
)
self.request.session['data_dict'] = data_dict
self.request.session['data_list'] = data_list
self.request.session.modified = True
return form_data
After this method is ran my new session keys aren't part of the session. From what I've been reading, this is a valid way of setting session data, but have I made a mistake somewhere?
Of the top of my head, your process_step function call misses the explicit request parm. It's mostly called like this:
process_step(self, request, form, step):

How to save a non ModelForm form to database in Django

I'm a newbie Django user, struggling with submitting form data to the database. So that I can generate dynamic forms I am using a non-ModelForm form to capture field data.
I'm commented out validation for now but I am trying to capture the POST data from the form to the database. The latest 'draft' of my views.py code is as follows - most interested in format from form_to_save = Scenario(...):
def scenario_add(request, mode_from_url):
urlmap = {
'aviation': 'Aviation',
'maritime': 'Maritime',
'international_rail': 'International Rail',
'uk_transport_outside_london': 'UK Transport (Outside London)',
'transport_in_london': 'Transport in London',
}
target_mode = urlmap[mode_from_url]
m = Mode.objects.filter(mode=target_mode)
tl = m[0].current_tl.threat_l
scenario_form = ScenarioForm(request.POST or None, current_tl=tl, mode=target_mode)
if request.method == 'POST':
#if scenario_form.is_valid():
form_to_save = Scenario(
target = Target(descriptor = scenario_form.fields['target']),
t_type = ThreatType(t_type = scenario_form.fields['t_type']),
nra_reference = NraReference(nra_code = scenario_form.fields['nra_reference']),
subt = scenario_form.fields['subt'],
ship_t = ShipType(ship_t = scenario_form.fields['ship_t']),
likelihood = scenario_form.fields['likelihood'],
impact = scenario_form.fields['impact'],
mitigation = scenario_form.fields['mitigation'],
compliance_score = scenario_form.fields['compliance_score'],
notes = scenario_form.fields['notes']
)
form_to_save.save()
# This needs to be changed to a proper redirect or taken to
# a scenario summary page (which doesn't yet exit.)
return render(request, "ram/new_scenario_redirect.html", {
'scenario_form': scenario_form,
'mode': mode_from_url,
'mode_proper': target_mode
})
else:
# if there is no completed form then user is presented with a blank
# form
return render(request, 'ram/scenario_add.html', {
'scenario_form': scenario_form,
'current_tl': tl,
'mode': mode_from_url,
'mode_proper': target_mode
})
Any advice gratefully received. Many thanks.
You've commented out the is_valid check, for some reason. You need that: as well as checking for validity, it also populates the form's cleaned_data dictionary which is what you should be getting the data from to create your object. (And don't call that object "form_to_save": it's a model instance, not a form).
if request.method == 'POST':
if scenario_form.is_valid():
scenario = Scenario(
target = Target(descriptor=scenario_form.cleaned_data['target']),
t_type = ThreatType(t_type = scenario_form.cleaned_data['t_type'])
...etc
Plus, you should move the final "return render" call out of the else block, so that it is caught when the form is not valid, to show any errors.
However, as petkostas says, you would almost certainly be better off using an actual ModelForm in the first place.
You can add custom options by overriding the init function in your form. For example:
class SomeForm(forms.Form):
department = forms.ChoiceField(widget=forms.Select, required=True)
...
def __init__(self, *args, **kwargs):
super(SomeForm, self).__init__(*args, **kwargs)
self.fields['department'].choices = Department.objects.all().order_by('department_name).values_list('pk', 'department_name')
You can also change the queryset in the init function:
where Department is a foreign key for example
queryset = Department.objects.filter(your logic)
self.fields['department'].queryset = queryset

Django Queryset sort by order_by with relatedManager

I am tryint to get objects sorted. this is my code:
ratings = Rate.objects.order_by(sortid)
locations = Location.objects.filter(locations_rate__in=ratings).order_by('locations_rate').distinct('id')
this is my model:
class Rate(models.Model):
von_location= models.ForeignKey(Location,related_name="locations_rate")
price_leistung = models.IntegerField(max_length=5,default=00)
bewertung = models.IntegerField(max_length=3,default=00)
how can I get all Locations in that order which is equal to that of ratings?
what I have above isnot working.
EDIT:
def sort(request):
sortid = request.GET.get('sortid')
ratings = Rate.objects.all()
locations = Location.objects.filter(locations_rate__in=ratings).order_by('locations_rate__%s' % sortid).distinct('id')
if request.is_ajax():
template = 'resultpart.html'
return render_to_response(template,{'locs':locations},context_instance=RequestContext(request))
You must specify a field to use for sorting the Rate objects, for example:
ratings = Rate.objects.all()
locations = Location.objects.filter(
locations_rate__in=ratings
).order_by('locations_rate__%s' % sortid).distinct('id')
You do not need to sort ratings beforehand.
The documentation provides example of use of order_by on related fields.

Categories