Tom
Tom

Reputation: 22841

Filtering a Class and Subclass in Django

I have a project with an FAQ app. The app has models for FAQ (written by the site authors) and UserFAQ (written by users-- not just a clever name). I want to return all entries, FAQ or UserFAQ that match certain conditions, but I also want to exclude any UserFAQs that don't match a certain criteria. Ideally, it would looks something like:

faqs = FAQ.objects.filter(question__icontains=search).exclude(show_on_site=False)

Where "show_on_site" is a property that only UserFAQ objects have. That doesn't work because the filter craps out on the parent class as it doesn't posses the property. What's the best way of doing this? I came across this snippet, but it seems like overkill for what I want to do.

Upvotes: 0

Views: 1069

Answers (1)

Dave W. Smith
Dave W. Smith

Reputation: 24966

In your position, absent a need to have two tables, I'd be tempted to have one FAQ model/table with is_user_faq and show_on_site fields.

Sometimes it helps when modeling data to organize it for simple and fast access. While model inheritance has some appeal, I've found it it's often easier to avoid using it.

Upvotes: 1

Related Questions