テンプレートに
<ul> {% if big_cat %} <li><a href="?{% url_replace request 'category' big_cat.pk %}">{{ big_cat.name }}</a></li> {% for small_cat in big_cat.children.all %} <li>---<a href="?{% url_replace request 'category' small_cat.pk %}">{{ small_cat.name }}</a></li> {% endfor %} {% else %} {% for big_cat in big_cats %} <li><a href="?{% url_replace request 'category' big_cat.pk %}">{{ big_cat.name }}</a></li> {% endfor %} {% endif %} </ul> <p>[ 場所 ]</p> <ul> {% if state %} <li><a href="?{% url_replace request 'location' state.pk %}">{{ state.name }}</a></li> {% if region %} <li>---<a href="?{% url_replace request 'location' region.pk %}">{{ region.name }}</a></li> {% for city in region.children.all %} <li>------<a href="?{% url_replace request 'location' city.pk %}">{{ city.name }}</a></li> {% endfor %} {% else %} {% for region in regions %} <li>---<a href="?{% url_replace request 'location' region.pk %}">{{ region.name }}</a></li> {% endfor %} {% endif %} {% else %} {% for state in states %} <li><a href="?{% url_replace request 'location' state.pk %}">{{ state.name }}</a></li> {% endfor %} {% endif %} </ul>
のような絞り込みをするサイドバーがあり、各リンクをクリックすることによって
class PostQuerySet(models.QuerySet): def filter_category(self, category): if not category.parent: return self.filter(category__in=category.children.all()) else: return self.filter(category=category) def filter_location(self, location): if not location.parent: # first return self.filter(location__parent__in=location.children.all()) else: if not location.dispatch.parent: # second return self.filter(location__in=location.children.all()) else: # third return self.filter(location=location) class Post(models.Model): category = models.ForeignKey(Category) location = models.ForeignKey(Location) price = models.DecimalField(max_digits=10, decimal_places=2) title = models.CharField(max_length=255) text = models.CharField(max_length=255) objects = PostQuerySet.as_manager() def __str__(self): return self.title
のようにcategoryやlocationのForeignKeyをもったPostが絞り込みされて表示されるようにいたいのですが、viewでどのように記述すればいいのでしょうか?
現在
def
1 category_pk = request.GET.get('category') 2 location_pk = request.GET.get('location') 3 4 c = {'posts': Post.objects.all()} 5 6 if category_pk: 7 category = get_object_or_404(Category, pk=category_pk) 8 c.update({'posts': Post.objects.filter_category(category)}) 9 10 if location_pk: 11 location = get_object_or_404(Location, pk=location_pk) 12 c.update({'posts': Post.objects.filter_location(location)})
のように記述しており、もしURLに?category=(カテゴリのpk)
や?category=(カテゴリのpk)&location=(場所のpk)
とあれば、viewにてPostを絞り込んで結果を返すようにしていますが、もし?category=(カテゴリのpk)&location=(場所のpk)
というようにGETパラメータが複数存在すると最後のGETパラメータのみ(この場合locationのみ)で絞り込みされます。
GETパラメータが1つでも複数でもその都度Postを絞り込み表示したいです。
一応
Post.objects.filter_category(category).filter_location(location)
とすればGETパラメータがcategoryとlocationの複数あってもそれに応じて絞り込みはできるのですが、この場合
if category_pk and location_pk: category = get_object_or_404(Category, pk=category_pk) location = get_object_or_404(Location, pk=location_pk) posts = Post.objects.filter_category(category).filter_location(location) elif category_pk: category = get_object_or_404(Category, pk=category_pk) posts = Post.objects.filter_category(category) elif location_pk: location = get_object_or_404(Location, pk=location_pk) posts = Post.objects.filter_location(location) else: posts = Post.objects.all() c.update({'posts': posts})
と多岐に渡る条件分岐をしなくてはいけません。また将来的にcategoryやlocationのみならず、他にも絞り込み条件を増やした場合、より複雑な条件分岐を記述しなくてはいけないので他の方法を模索しているのですが僕の今の知識では到底実装できそうにありません。
理想としては
# GETパラメータがないときは Post.objects.all() # categoryがあるときは Post.objects.filter_category(category) # locationがあるときは Post.objects.filter_location(location) # 複数個あるときはその都度自動的にfilterを追加する Post.objects.filter().filter().filter()......
という具合です。
filter_categoryやfilter_locationといった仕組み自体に問題があればこれも改善したいのですが、絞り込み元のモデルが親をもつ時や親の親を持つ時等、複雑なので他にいいアイデアが思いつきません。
どうかご教授願います。
categoryとlocationのモデルです。
class Category(models.Model): parent = models.ForeignKey('Category', related_name='children', null=True, blank=True) name = models.CharField(max_length=255) slug = models.SlugField() @property def dispatch(self): # parent が nullであれば親カテゴリなので自分自身を返す if not self.parent: return self # そうでなければ親カテゴリ(parent)を返す else: return self.parent def __str__(self): return self.name class Location(models.Model): parent = models.ForeignKey('Location', related_name='children', null=True, blank=True) name = models.CharField(max_length=255) @property def dispatch(self): if not self.parent: return self else: return self.parent def __str__(self): return self.name
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。