How do I search a db field for a the string after a "#" and add it to another db field in django - django

I want to have a content entry block. When a user types #word or #blah in a field, I want efficiently search that field and add the string right after the "#" to a different field as a n entry in a different table. Like what Twitter does. This would allow a user to sort by that string later.
I believe that I would do this as a part of the save method on the model, but I'm not sure. AND, if the #blah already exists, than the content would belong to that "blah"
Can anyone suggest samples of how to do this? This is a little beyond what I'm able to figure out on my own.
Thanks!

You can use regex (re) during save() or whenever to check if your field text contains #(?P<blah>\w+) , extract your blah and and use it for whatever you want .

Related

django split data and apply search istartswith = query

I have a Project and when searching a query I need to split the data (not search query) in to words and apply searching.
for example:
my query is : 'bot' (typing 'bottle')
but if I use meta_keywords__icontains = query the filter will also return queries with 'robot'.
Here meta_keywords are keywords that can be used for searching.
I won't be able to access data if the data in meta_keywords is 'water bottle' when I use meta_keywords__istartswith is there any way I can use in this case.
what I just need is search in every words of data with just istartswith
I can simply create a model for 'meta_keywords' and use the current data to assign values by splitting and saving as different data. I know it might be the best way. I need some other ways to achieve it.
You can search the name field with each word that istartswith in variable query.
import re
instances = Model.objects.filter(Q(name__iregex=r'[[:<:]]' + re.escape(query)))
Eg: Hello world can be searched using the query 'hello' and 'world'. It don't check the icontains
note: It works only in Python3

Query Django's HStoreField values using LIKE

I have a model with some HStoreField attributes and I can't seem to use Django's ORM HStoreField to query those values using LIKE.
When doing Model.objects.filter(hstoreattr__values__contains=['text']), the queryset only contains rows in which hstoreattr has any value that matches text exactly.
What I'm looking for is a way to search by, say, te instead of text and those same rows be returned as well. I'm aware this is possible in a raw PostgreSQL query but I'm looking for a solution that uses Django ORM.
If you want to check value of particular key in every object if it contains 'te', you can do:
Model.objects.filter(hstoreattr__your_key__icontains='te')
If you want to check if any key in your hstore field contains 'te', you will need to create your own lookup in django, because by default django won't do such thing. Refer to custom lookups in django docs for more info.
As far as I can remember, you cannot filter in values. If you want to filter in values, you have to pass a column and value you are referencing to. When you want it to be case insensitive use __icontains.
Although you cannot filter by all values, you can filter by all keys. Just like you showed in your code.
If you want to search for 'text' in all objects in key named let's say 'fo' - just do smth like this:
Model.objects.filter(hstoreattr__icontains={'fo': 'text'})

How to compare and substitute strings in Ruby on Rails?

Trying to build a barebones concept in Ruby on Rails that will take a string, map each individual word in this string, compare it and then substitute the word if it matches predefined strings in related databases.
For example: User inputs in text field "What does lol and brb mean?" Hits Submit button. The action gives back the same text with "lol" and "brb" changed to "laughing out loud" and "be right back".
So far I have a Post model & table for the User input that stores the string in the database.
I have an Acronym model & table that has "lol" and "brb" stored in database with a foreign key reference to Acronym_Translate model & table that has "laughing out loud" and "be right back" referenced to "lol" and "brb", respectively.
How would I connect the Post model/table to the Acronym model/table in order to compare the strings in Post and substitute with strings from Acronym model/table? And what command could achieve such function? Would gsub! method work here?
Any help would be appreciated!
Are you sure that you want to connect the Post table to the Acronym table? This means that you would have to identify and keep a record of each instance of an acronym within a post.
You can do this using a many to many relation or if you want to store extra data about each acronym occurrence you should create a link table named AcronymPost and use a has many through relationship between Post and Acronym. When you parse a post value, and when you identify an acronym in the post, you would have to record this in the database and then use gsub to replace the post value with the acronym.
You can iterate through your table of acronyms and use (string).include? method to check if it occurs in the post. Finally, you could use a gsub command to replace the acronym with its translation.

Custom field / form field (maybe widget?) for storing values for a string format

I wish to create a model where I can store a string formatting pattern as well as accompanying values.
Example:
Pattern = 'Strength deals %d more damage for each %f%% critical chance'
Values = [2, 1.50]
The pattern only has to store ints and floats. My initial thought was to simply create a custom field, validate the values and that would be it. You can see my field here
However, this simply lets me save the list. I still need some way to figure out how many values to validate, check that it matches the other field (a simple CharField). This could be done "manually", but I would like to create a custom form field that would generate X input boxes that match the number of wildcards in the pattern.
Question 1: Is there any way to "link" two fields so that one can act on the value of the other?
Question 2: To create this, I would probably need to create a new form widget, but is this possible? Any hints as to how to start?
This may not exact answer, but writing it in comment is not feasible.
Another option would to store as JSON string in the model. Like
[{ "Pattern": 'Strength deals %d more damage for each %f%% critical chance',
"Values" : [2, 1.50]
}]
Use custom form field to input/output as JSON input. There are quite a few implementation available when searched on google.
With this approach, you can try to validate the inputs, so that format string and number of variables provided matches.

How to get field name

Recently i have implemented django-sphinx search on my website.
It is working fine of each separate model.
But now my client requirement has changed.
To implement that functionality i need field name to whom search is made.
suppose my query is:
"select id, name,description from table1"
and search keyword is matched with value in field "name". So i need to return that field also.
Is it possible to get field name or any method provided by django-sphinx which return field name.
Please help me...
As far as I know, this isn't possible. You might look at the contents of _sphinx though.
Well from django-sphinx it might not be possible. But there is a solution -
Make different indexes, each index specifying the field that you need to search.
In your django-sphinx models while searching do this -
search1 = SphinxSearch(index='index1')
search2 = SphinxSearch(index='index2')
...
After getting all the search results, you aggregate them & you have the info of from where they have come.