I have this django class-based view where i am trying to overwrite the get_queryset function because i want get to the inserted values from the frontend to search in my database after the subject with that name and then get the id. but when i call the view it gives me a "Subject matching query does not exist." because the subject_val is None. That makes sense because the user has not submitted the values jet.. so how do i get it to wait until a user have choosen "submit
class AttendanceList(LoginRequiredMixin, ListView):
model = AttendanceLog
template_name = "./attendancecode/showattendance.html"
def get_queryset(self):
class_val = self.request.GET.get('class')
subject_val = self.request.GET.get('subject')
sub = Subject.objects.get(name=subject_val).id
new_context = get_statstic(class_val, sub)
return new_context
def get_context_data(self, **kwargs):
context = super(AttendanceList, self).get_context_data(**kwargs)
context['class'] = self.request.GET.get('class')
context['subject'] = self.request.GET.get('subject')
return context
You can check if the values are not None, in case they are, you need to return another queryset of AttendanceLogs (for example AttendanceLog.objects.all() or AttendanceLog.objects.none()):
class AttendanceList(LoginRequiredMixin, ListView):
model = AttendanceLog
template_name = "./attendancecode/showattendance.html"
def get_queryset(self):
class_val = self.request.GET.get('class')
subject_val = self.request.GET.get('subject')
if class_val is not None and subject_val is not None:
sub = Subject.objects.get(name=subject_val).id
return get_statstic(class_val, sub)
# return another queryset:
return AttendanceLog.objects.none()
# …
Related
I'm currently trying to convert my FBV codes to CBV. get_context_data is working well by returning contexts that I put in. However, get_queryset() returns NOTHING for some reason. To double-check, I tried to print search_stores right before returning it and it printed the queryset that is supposed to be printed. However, when I printed it on Django template, by typing {{ search_stores }}, it shows nothing. Am I using get_queryset in a wrong way?
class SearchListView(ListView):
model = Store
template_name = 'boutique/search.html'
# paginate_by = 5
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['search_text'] = self.request.GET.get('search_text')
context['sorter'] = self.request.GET.get('sorter')
if not context['sorter']:
context['sorter'] = 'popularity'
return context
def get_queryset(self):
search_text = self.request.GET.get('search_text')
sorter = self.request.GET.get('sorter')
if search_text:
search_stores = Store.objects.filter(Q(businessName__icontains=search_text) | Q(mKey__icontains=search_text))
if sorter == 'businessName':
search_stores = search_stores.order_by(sorter)
else:
search_stores = search_stores.order_by(sorter).reverse()
else:
search_stores = ''
for store in search_stores:
store.mKey = store.mKey.split(' ')
print(search_stores)
return search_stores
Your queryset is accessible via the context_object_name.
By default it's object_list if you don't provide context_object_name
You can access the queryset in templates with object_list
If you want to change the name, change the context_object_name:
class SearchListView(ListView):
model = Store
template_name = 'boutique/search.html'
context_object_name = 'search_stores'
search_stores will be the variable accessible to loop through in templates
this part of my code fills the queryset with [category_object].subcats.all(). let subcats be a method of category object:
serializer:
class CatSrlz(serializers.ModelSerializer):
class Meta:
model = Category
fields = ('id', 'label', )
View:
class CatsViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Category.objects.filter(parent=None)
serializer_class = CatSrlz
def retrieve(self, request, *args, **kwargs):
# return Response({'res': self.kwargs})
queryset = Category.objects.get(pk=str(self.kwargs['pk'])).subCats.all()
dt = CatSrlz(queryset, many=True)
return Response(dt.data)
and url:
router.register(r'cats', views.CatsViewSet)
it works but i'm pretty sure that there must be a more correct way of doing so
Is there one?
thanks
When retrieving a single object, you can use the get_object method in your view, which look like this in DRF without modifications :
def get_object(self):
"""
Returns the object the view is displaying.
You may want to override this if you need to provide non-standard
queryset lookups. Eg if objects are referenced using multiple
keyword arguments in the url conf.
"""
queryset = self.filter_queryset(self.get_queryset())
# Perform the lookup filtering.
lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
assert lookup_url_kwarg in self.kwargs, (
'Expected view %s to be called with a URL keyword argument '
'named "%s". Fix your URL conf, or set the `.lookup_field` '
'attribute on the view correctly.' %
(self.__class__.__name__, lookup_url_kwarg)
)
filter_kwargs = {self.lookup_field: self.kwargs[lookup_url_kwarg]}
obj = get_object_or_404(queryset, **filter_kwargs)
# May raise a permission denied
self.check_object_permissions(self.request, obj)
return obj
So you could adapt the part where you get your object :
filter_kwargs = {self.lookup_field: self.kwargs[lookup_url_kwarg]}
obj = get_object_or_404(queryset, **filter_kwargs)
and add your subcat logic there.
By the way, I don't get why you are using
dt = CatSrlz(queryset, many=True)
Shouldn't "retrieve" return a single object?
Using CreateView, I am trying to save and return a url based on the items selected in the form. This is for a workout log site where the second form you are directed to is based on the type of workout you want, based on info entered in the first form. I would like to do this without Javascript:
class WorkoutLogCreateView(CreateView):
model = Workout_Log
template_name = 'workout/workout_log_create.html'
fields = ['date_time', 'modality', 'workout_type', 'workout_model']
#something like this:
def get_success_url(self):
if workout_type = '5 rounds'
return reverse('five-rounds-log-create')
elif workout_type = 'Drop Reps'
return reverse('drop-reps-log-create')
else
return reverse('workout-log-menu')
def get_context_data(self, **kwargs):
context = super(WorkoutLogCreateView, self).get_context_data(**kwargs)
context['action'] = reverse('workout-log-create')
return context
In the get_success_url method, you can access the object with self.object.
def get_success_url(self):
if self.object.workout_type = '5 rounds'
return reverse('five-rounds-log-create')
elif self.object.workout_type = 'Drop Reps'
return reverse('drop-reps-log-create')
else
return reverse('workout-log-menu')
My Goal
A site that list all my Updates (model) in a table
Dont display all models at once (pagination - maybe 10 per page)
Filter and sort the list
My thoughts
I can use ListView to get a set of all my Updates
Use paginate_by = 10
Use a form to set order_by or filter in my QuerySet
My Problem
I am not sure how to add an form to modify my QuerySet with filter and sortings. My Idea was to modify the Query in get_queryset with additional filter and order_by.
My View
class MyView(ListView):
model = Update
template_name = "updates/update.html"
paginate_by = 10
def get_queryset(self):
return Update.objects.filter(
~Q(state=Update.STATE_REJECTED),
~Q(state=Update.STATE_CANCELED),
~Q(state=Update.STATE_FINISHED),
).order_by(
'planned_release_date'
)
My Idea
Something like this. I know it's not working like this ... just to illustrate
class MyView(ListView):
model = Update
template_name = "updates/update.html"
paginate_by = 10
def post(self, request, *args, **kwargs):
new_context = Update.objects.filter(
request.POST.get("filter"),
).order_by(
request.POST.get("sorting"),
)
def get_queryset(self):
return Update.objects.filter(
~Q(state=Update.STATE_REJECTED),
~Q(state=Update.STATE_CANCELED),
~Q(state=Update.STATE_FINISHED),
).order_by(
'planned_release_date'
)
You don't need post. Pass the filter value and order_by in the url for example:
.../update/list/?filter=filter-val&orderby=order-val
and get the filter and orderby in the get_queryset like:
class MyView(ListView):
model = Update
template_name = "updates/update.html"
paginate_by = 10
def get_queryset(self):
filter_val = self.request.GET.get('filter', 'give-default-value')
order = self.request.GET.get('orderby', 'give-default-value')
new_context = Update.objects.filter(
state=filter_val,
).order_by(order)
return new_context
def get_context_data(self, **kwargs):
context = super(MyView, self).get_context_data(**kwargs)
context['filter'] = self.request.GET.get('filter', 'give-default-value')
context['orderby'] = self.request.GET.get('orderby', 'give-default-value')
return context
Make sure you give proper default value to filter and orderby
Example form (you can modify this to your need):
<form method="get" action="{% url 'update-list' %}">
<p>Filter: <input type="text" value={{filter}} name="filter"/></p>
<p>order_by: <input type="text" value={{orderby}} name="orderby"/></p>
<p><input type="submit" name="submit" value="submit"/></p>
</form>
I am wondering why nobody mentioned here this cool library: django-filter https://github.com/carltongibson/django-filter
you can define your logic for filtering very clean and get fast working forms etc.
demo here: https://stackoverflow.com/a/46492378/953553
I posted this elsewhere but I think this adds to the selected answer.
I think you would be better off doing this via get_context_data. Manually create your HTML form and use GET to retrieve this data. An example from something I wrote is below. When you submit the form, you can use the get data to pass back via the context data. This example isn't tailored to your request, but it should help other users.
def get_context_data(self, **kwargs):
context = super(Search, self).get_context_data(**kwargs)
filter_set = Gauges.objects.all()
if self.request.GET.get('gauge_id'):
gauge_id = self.request.GET.get('gauge_id')
filter_set = filter_set.filter(gauge_id=gauge_id)
if self.request.GET.get('type'):
type = self.request.GET.get('type')
filter_set = filter_set.filter(type=type)
if self.request.GET.get('location'):
location = self.request.GET.get('location')
filter_set = filter_set.filter(location=location)
if self.request.GET.get('calibrator'):
calibrator = self.request.GET.get('calibrator')
filter_set = filter_set.filter(calibrator=calibrator)
if self.request.GET.get('next_cal_date'):
next_cal_date = self.request.GET.get('next_cal_date')
filter_set = filter_set.filter(next_cal_date__lte=next_cal_date)
context['gauges'] = filter_set
context['title'] = "Gauges "
context['types'] = Gauge_Types.objects.all()
context['locations'] = Locations.objects.all()
context['calibrators'] = Calibrator.objects.all()
# And so on for more models
return context
This is how we do it, that way you get validation/type conversion as well:
class UnitList(PermissionRequiredMixin, ListView):
""" Class based view to show a list of all buildings for a specific user """
model = Unit
ordering = ['building', 'unit']
paginate_by = 100
# Access
permission_required = ['core.manager_perm']
raise_exception = True # If true, give access denied message rather than redirecting to login
def get_queryset(self):
try:
units = self.model.objects.filter(building__company=self.request.user.profile.company)
except Profile.DoesNotExist:
units = self.model.objects.none()
form = UnitSearchForm(self.request.GET)
if form.is_valid():
filters = {}
address = form.cleaned_data['address']
neighborhood = form.cleaned_data['neighborhood']
beds = form.cleaned_data['beds']
amenity = form.cleaned_data['amenity']
if address:
filters['building__street_index__istartswith'] = compute_street_address_index(address)
if neighborhood:
filters['building__neighborhood__icontains'] = neighborhood
if beds:
filters['beds'] = beds
if amenity:
filters['unit_amenities__name__iexact'] = amenity
units = units.filter(**filters)
return units.select_related('building').order_by(*self.ordering)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['form'] = UnitSearchForm(self.request.GET)
return context
I'm looking for a way to add a 'generic' search throught some of my ModelResource.
Using a 'v1' api, I would like to be able to query some of my ModelResources allready registered with this kind of url : /api/v1/?q='blabla'. Then I'd like to recover some of my ModelResourceS that could fill inside the query.
What approach do you think is the best one ?
I tried to build a GenericResource(Resource), with my own class reprensenting row data, without success. Would you have got some links to help me ?
Regards,
For a mobile application that we were creating an API for we created a similar "Search" type resource. Basically we agreed upon a set of types and some common fields that we would show in the search feed on the application. See the code below for the implementation:
class SearchObject(object):
def __init__(self, id=None, name=None, type=None):
self.id = id
self.name = name
self.type = type
class SearchResource(Resource):
id = fields.CharField(attribute='id')
name = fields.CharField(attribute='name')
type = fields.CharField(attribute='type')
class Meta:
resource_name = 'search'
allowed_methods = ['get']
object_class = SearchObject
authorization = ReadOnlyAuthorization()
authentication = ApiKeyAuthentication()
object_name = "search"
include_resource_uri = False
def detail_uri_kwargs(self, bundle_or_obj):
kwargs = {}
if isinstance(bundle_or_obj, Bundle):
kwargs['pk'] = bundle_or_obj.obj.id
else:
kwargs['pk'] = bundle_or_obj['id']
return kwargs
def get_object_list(self, bundle, **kwargs):
query = bundle.request.GET.get('query', None)
if not query:
raise BadRequest("Missing query parameter")
#Should use haystack to get a score and make just one query
objects_one = ObjectOne.objects.filter(name__icontains=query).order_by('name').all)[:20]
objects_two = ObjectTwo.objects.filter(name__icontains=query).order_by('name').all)[:20]
objects_three = ObjectThree.objects.filter(name__icontains=query).order_by('name').all)[:20]
# Sort the merged list alphabetically and just return the top 20
return sorted(chain(objects_one, objects_two, objects_three), key=lambda instance: instance.identifier())[:20]
def obj_get_list(self, bundle, **kwargs):
return self.get_object_list(bundle, **kwargs)