0

I'd like to extend the FlatPage model in Django so that I can implement searching within its field. That's what I've done:

models.py:

from django.db.models import Q
from django.contrib.flatpages.models import FlatPage

class FlatPageManager(models.Manager):
    def search(self, query=None):
        qs = self.get_queryset()
        if query is not None:
            or_lookup = (Q(title__icontains=query) |
                        Q(content__icontains=query)
                        )
            qs = qs.filter(or_lookup).distinct()
        return qs

class MyFlatPage(FlatPage):

    objects = FlatPageManager()

views.py:

class SearchView(ListView):
   [...]
    def get_context_data(self, *args, **kwargs):

        context['query'] = self.request.GET.get('q')
        return context

    def get_queryset(self):
        request = self.request
        query = request.GET.get('q', None)

        if query is not None:
            flatpage_results     = MyFlatPage.objects.search(query)


            qs = sorted(queryset_chain,
                        key=lambda instance: instance.pk,
                        reverse=True)
            return qs

The above search method works for other models I have, so it should also work for MyFlatPage. Nevertheless, I get no results coming from that query. Am I missing something?

Update 1

What is empty is the whole MyFlatPage list:

>>> MyFlatPage.objects.all()
<QuerySet []>
HBMCS
  • 686
  • 5
  • 25
  • Am I understand correct: you have no result in you api? you have got an empty list instead? – ncopiy Jun 15 '20 at 22:10
  • @ncopiy I get an empty list: `>>> MyFlatPage.objects.search('the') ` (there are definitely contents with 'the'). Actually I get an empty list of objects.all too: `>>> MyFlatPage.objects.all() `, so the issue is the MyFlatPage class. – HBMCS Jun 15 '20 at 22:30
  • Ok I have three versions. 1) Maybe there is nothing in DB, could you please check your `or` filtrations separately? 2) `request.GET.get('q', None)` - maybe replace it by `request.query_params` (I know there are synonyms but maybe) 3) You may replace `sorted` method usage by `qs = qs.order_by("-id")`. – ncopiy Jun 15 '20 at 22:44
  • @ncopiy Thank you for your suggestions. As I said, though, my 'pure' .objects.all() manager is empty, so it's not a search method issue (which works for other models anyway). Shouldn't `MyFlatPage` inherit all the objects and fields from `FlatPage`? `FlatPage.objects.all()` is well stocked-up with many objects. – HBMCS Jun 15 '20 at 22:50
  • 1
    It depends. If you've defined `MyFlatPage` as proxy model, then you will have the same table in database as for `FlatModel` as for `MyFlatModel`. If you have not defined `proxy = True` in `Meta` then you have another table and all flats from `FlatModel` are not located in `MyFlatModel` table. Check here: https://docs.djangoproject.com/en/3.0/topics/db/models/#proxy-models – ncopiy Jun 15 '20 at 22:55
  • 1
    Spot on. As you can see from my code I was missing the `proxy=True` in `Meta`. If you write up an answer I'll accept it. – HBMCS Jun 15 '20 at 23:00

1 Answers1

0

There is two common types for django models inheritance: the proxy-models and as a new models.

If you want to use FlatPage table as a table for new model - MyFlatPage you have to define MyFlatPage as a proxy one by adding proxy = True flag in Meta:

class MyFlatPage(FlatPage):
    class Meta:
        proxy = True

In other case you will have another table for MyFlatPage.

HBMCS
  • 686
  • 5
  • 25
ncopiy
  • 1,515
  • 14
  • 30