Skip to content
Advertisement

Django query for column value search?

What is the Django query for this?

DB data –

JavaScript

I only need record that contain “1.2.3” values not like – (“Cat 1.2.3” or “1.2.3-XY2” or any such value). And pattern “1.2.3” can be anywhere in column where column value can have comma separated values too.

Desired Result –

JavaScript

When i am performing below Django query –

JavaScript

Getting all record but i only need record that contain “1.2.3” values not like – (“Cat 1.2.3” or “1.2.3-XY2” or any such value).

How do I implement this in Django?

Advertisement

Answer

Your data structure is a bit odd, in my opinion you should have separate objects for each of those entries rather than keeping potentially comma-separated values. However, you can do this by joining together Django Q() objects:

  1. Complete match to “1.2.3”, or
  2. Results that contain “,1.2.3”, or
  3. Results that contain “, 1.2.3”
JavaScript

Because you have a variety of possible options, you need to string a few Q() options together. The | character is treated as OR.

Edit – per comments

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement