django-filters

Django filter query if filter parameter exists

情到浓时终转凉″ 提交于 2021-02-10 19:55:21
问题 I'm trying to create a django filter with multiple filter parameters (ex. name, age, height). However, I only want to filter by a parameter if it exists... In my init: def __init__(self, name=None, age=None, height=None): self.name = name self.age = age self.height = height in my query: Person.objects.filter(name=self.name, age=self.age, height=self.height) However, the problem is since the parameters are optional in the constructor, there is a chance that the filter is looking for None

Django: TemplateSyntaxError, Invalid filter

僤鯓⒐⒋嵵緔 提交于 2020-12-30 09:51:07
问题 I'm trying to access a value from a dictionary using a variable, all in an HTML file that follows Django's Templating language. Django's templating language doesn't let you access a dictionary using a variable as a key, so I decided to use Django filters to do it. However, I am getting a "Invalid filter" error and I can't figure out why. My html file <table class="table"> <tbody> <tr> <th scope="row">Username</th> <th scope="row">Full Name</th> <th scope="row">Points</th> {% for subject in

Django filters to return HTML that will be rendered in the template

梦想的初衷 提交于 2020-08-07 07:53:29
问题 my_text my_text = '''The template system works in a two-step process: compiling and rendering. A compiled template is, simply, a list of Node objects. Thus, to define a custom template tag, you specify how the raw template tag is converted into a Node (the compilation function), and what the node’s render() method does.''' my_filter @register.filter(is_safe=True) def format_description(description): text = '' for i in description.split('\n'): text += ('<p class="site-description">' + i + '</p

How to send a variable from html page back to Django view for further processing

江枫思渺然 提交于 2020-01-25 07:32:06
问题 I am using jQuery autocomplete search (shown below): def CitySearch(request): country_search_id = <country_id> # "country_id" to be sent from html page (value stored in a field) if request.is_ajax(): q = request.GET.get('term','') names = City.objects.filter(country__id=country_search_id).filter(name__icontains=q).annotate(value=F('name'), label=F('name')).values('id', 'value', 'label') result = list(names) data = json.dumps(result) mimetype = 'application/json' return HttpResponse(data,

Reload choices dynamically when using MultipleChoiceFilter

你离开我真会死。 提交于 2020-01-03 09:52:30
问题 I am trying to construct a MultipleChoiceFilter where the choices are the set of possible dates that exist on a related model ( DatedResource ). Here is what I am working with so far... resource_date = filters.MultipleChoiceFilter( field_name='dated_resource__date', choices=[ (d, d.strftime('%Y-%m-%d')) for d in sorted(resource_models.DatedResource.objects.all().values_list('date', flat=True).distinct()) ], label="Resource Date" ) When this is displayed in a html view... This works fine at

Filtering in django rest framework

房东的猫 提交于 2020-01-01 05:10:09
问题 In my project I use django rest framework. To filter the results I use django_filters backend. There is my code: models.py from django.db import models class Region(models.Model): name = models.CharField(max_length=100, blank=True, null=False) class Town(models.Model): region = models.ForeignKey(Region) name = models.CharField(max_length=100, blank=True, null=False') filters.py import django_filters from models import Town class TownFilter(django_filters.FilterSet): region = django_filters

Django Templates First element of a List

…衆ロ難τιáo~ 提交于 2019-12-20 16:58:43
问题 I pass a dictionary to my Django Template, Dictionary & Template is like this - lists[listid] = {'name': l.listname, 'docs': l.userdocs.order_by('-id')} {% for k, v in lists.items %} <ul><li>Count: {{ v.docs.count }}, First: {{ v.docs|first }}</li></ul> {% endfor %} Now docs is a list of userdocs type. i.e. is an instance. So first filter returns me this instance. From this I need to extract it's id . How do I do that? I tried {{ v.docs|first }}.id and various other futile trials. 回答1: You

Django Templates First element of a List

大憨熊 提交于 2019-12-20 16:58:32
问题 I pass a dictionary to my Django Template, Dictionary & Template is like this - lists[listid] = {'name': l.listname, 'docs': l.userdocs.order_by('-id')} {% for k, v in lists.items %} <ul><li>Count: {{ v.docs.count }}, First: {{ v.docs|first }}</li></ul> {% endfor %} Now docs is a list of userdocs type. i.e. is an instance. So first filter returns me this instance. From this I need to extract it's id . How do I do that? I tried {{ v.docs|first }}.id and various other futile trials. 回答1: You

Mezzanine BlogCategory parsing categories

时光毁灭记忆、已成空白 提交于 2019-12-14 02:20:10
问题 I want to build a custom filter that takes a blog_post as an argument and does some parsing of the categories (attached to the blog post). I tried like this: from mezzanine import template from mezzanine.blog.models import BlogPost, BlogCategory register = template.Library() @register.filter(name='has_friends') def has_friends(blog_post): categories = blog_post.categories.all() if 'Friends' in categories: return False else: return True The problem is that blog_post.categories.all() returns

OR definition of filters when using relations in django filter

 ̄綄美尐妖づ 提交于 2019-12-13 03:44:49
问题 I have three models with a simple relation as below: models.py class Person(models.Model): first_name = models.CharField(max_length=20) last_name = models.CharField(max_length=20) class PersonSession(models.Model): start_time = models.DateTimeField(auto_now_add=True) end_time = models.DateTimeField(null=True, blank=True) person = models.ForeignKey(Person, related_name='sessions') class Billing(models.Model): DEBT = 'DE' BALANCED = 'BA' CREDIT = 'CR' session = models.OneToOneField