Add labels to estimator.export_saved_model when exporting a keras model for google cloud - tensorflow

I am trying to export a hdf5 model created by Keras training to Google cloud ML Engine. I have everything except the labels after making an online prediction and I would like to have the labels with probability after making a prediction.
Here is my code after the training and the creation of an hdf5 model with Keras.
First, I create an estimator from a keras model.
estimator = keras.estimator.model_to_estimator(
keras_model_path="model.hdf5",
model_dir="output/")
Now, I export the model like this:
estimator.export_saved_model(
"output/model"
serving_input_receiver_fn=serving_input_receiver_fn)
with the function serving_input_receiver_fn that will allow me to accept a base 64 json file as input for an online prediction with google cloud.
def serving_input_receiver_fn():
def prepare_image(image_str_tensor):
image = tf.image.decode_jpeg(image_str_tensor, channels=3)
return image_preprocessing(image)
input_ph = tf.placeholder(tf.string, shape=[None])
images_tensor = tf.map_fn(
prepare_image, input_ph, back_prop=False, dtype=tf.uint8)
images_tensor = tf.image.convert_image_dtype(images_tensor,
dtype=tf.float32)
return tf.estimator.export.ServingInputReceiver(
{'input_1': images_tensor},
{'image_bytes': input_ph})
However, I want to have a classification result with the labels ( I have around 10 classes in my results ). Now my only result is like this :
{"input_1": [0.001,0.9,...]}
I would like to have the result with the labels. Is it possible to do it with a small change and not by doing an other training but by keeping my hdf5 model file ?
Thank you in advance.

We had similar requirement for a demo:
I have a dictionary of classes:
https://gist.github.com/yrevar/942d3a0ac09ec9e5eb3a
{0: 'tench, Tinca tinca',
1: 'goldfish, Carassius auratus', ...}
and so on.
I did the conversion at client level:
def get_classes():
url = 'https://gist.githubusercontent.com/yrevar/942d3a0ac09ec9e5eb3a/raw' \
'/238f720ff059c1f82f368259d1ca4ffa5dd8f9f5' \
'/imagenet1000_clsidx_to_labels.txt'
response = requests.get(url)
classes = literal_eval(response.text)
return classes
...
classes = get_classes()
response = model_predict(predict_request)
if response:
prediction_class = response.get('predictions')[0].get('classes') - 1
prediction_probabilities = response.get('predictions')[0].get('probabilities')
print(
'Prediction: [%d] %s Probability [%.2f] ' % (
prediction_class, classes[prediction_class], max(prediction_probabilities)))
Code here:
https://github.com/GoogleCloudPlatform/ml-on-gcp/blob/master/dlvm/nvidia/inference.py
Response looks like this:
{'predictions': [{'probabilities': [1.55508e-05, 8.52272e-05, 0.000124575, 0.000202289, 7.25561e-05, 0.00125153, 0.000195685, 0.000298364, 6.305e-05, 0.000101759, 0.000189796, 9.83266e-06, 6.09115e-06, 2.93628e-05, 3.79306e-05, 1.80906e-05, 3.27449e-05, 1.44569e-05, 2.08072e-05, 0.000211307, 2.92737e-05, 2.62217e-05, 5.72919e-05, 0.000113042, 2.7489e-05, 4.75314e-05, 3.24912e-05, 9.47271e-06, 0.000175823, 1.07195e-05, 2.23769e-05, 2.77867e-05, 9.41769e-06, 2.75326e-06, 5.0539e-05, 0.000196899, 1.57362e-05, 9.59799e-05, 3.38195e-05, 7.26347e-06, 6.13557e-05, 5.6595e-05, 2.1883e-05, 3.92613e-05, 2.65449e-05, 5.75036e-05, 0.000152569, 8.00665e-05, 2.52358e-05, 7.63134e-05, 1.58771e-05, 0.00046693, 8.97672e-05, 2.64159e-05, 0.000107967, 0.000105322, 2.51052e-05, 0.000134213, 2.02501e-05, 8.42264e-05, 5.74879e-05, 0.000147237, 8.60201e-05, 0.000159229, 2.82999e-05, 7.0453e-05, 9.804e-05, 1.53984e-05, 0.000442353, 4.83388e-05, 0.000111974, 1.64856e-05, 3.9036e-05, 8.38488e-06, 8.2569e-05, 4.60937e-05, 1.9807e-05, 0.000101196, 0.00014236, 0.000169874, 0.000836153, 9.40354e-05, 4.6951e-05, 0.000131597, 2.86648e-05, 0.000158368, 5.29119e-05, 3.52403e-05, 7.17581e-05, 0.000116447, 0.000253711, 5.35324e-05, 8.56567e-06, 4.87063e-05, 0.000110679, 2.18005e-05, 8.59478e-06, 7.40535e-05, 2.38494e-05, 3.12719e-05, 0.000714874, 0.000145422, 0.000137946, 9.94839e-05, 0.000283478, 0.000357132, 2.73016e-05, 0.0002482, 8.15625e-05, 7.40048e-05, 3.81499e-05, 9.95147e-06, 2.86458e-05, 6.22204e-05, 0.000123885, 8.62779e-05, 3.16152e-05, 2.91354e-05, 5.67827e-05, 0.000652813, 0.000101906, 1.61919e-05, 2.92731e-05, 4.40727e-05, 8.18691e-06, 2.21699e-05, 5.32086e-05, 3.21545e-05, 3.22796e-05, 2.6318e-05, 1.88785e-05, 2.11514e-05, 1.48076e-05, 7.21377e-05, 7.36493e-06, 0.000353744, 0.000141821, 8.97949e-06, 1.61471e-05, 0.000122686, 4.4602e-05, 2.3205e-05, 4.94825e-05, 1.67007e-05, 6.61634e-05, 8.84246e-05, 0.000172353, 7.35944e-05, 0.000391683, 0.000185004, 0.00039224, 0.000324578, 0.000331324, 1.30768e-05, 0.000374572, 9.66308e-05, 0.000116723, 9.00387e-05, 9.85038e-05, 6.74917e-05, 0.000493128, 7.29576e-05, 0.00450054, 0.00298045, 0.00203722, 0.00164224, 0.000846203, 0.00017548, 0.000317891, 0.000788288, 0.000143928, 0.000854663, 0.000351869, 0.000692566, 0.000377429, 0.000629245, 6.21199e-05, 0.000200465, 0.00307031, 0.000723996, 0.000117597, 0.000115785, 5.71359e-05, 0.000496389, 8.51815e-05, 0.000474041, 0.000364161, 2.79947e-05, 6.08602e-05, 0.000209289, 0.00255232, 8.92871e-05, 0.00296907, 0.000557994, 3.34299e-05, 2.18733e-05, 0.00575903, 0.000440953, 0.00127525, 6.44013e-05, 0.0101199, 0.000215603, 4.69506e-05, 0.000803412, 0.0136009, 0.000157733, 0.000238714, 0.000401412, 0.000817349, 0.00053235, 0.00576978, 0.0001202, 0.00019474, 0.00292783, 0.000253787, 0.000986474, 0.000137718, 3.35719e-05, 0.00109508, 3.28824e-05, 0.00050906, 8.51038e-05, 0.000519757, 0.000235538, 0.00788667, 0.00105736, 0.000530407, 0.000325091, 0.00107374, 0.000230244, 0.00076778, 0.000659183, 0.000224508, 0.000202612, 0.000678197, 0.00448022, 0.00289446, 9.32325e-05, 0.00977335, 0.000435942, 0.004958, 0.000386235, 0.00736235, 0.00115626, 0.000177735, 2.56265e-05, 0.0015713, 0.000136755, 0.000602412, 0.0012884, 0.00719902, 0.0119457, 0.000945569, 0.000219249, 0.000347739, 0.00519515, 0.000225742, 0.00246375, 0.000453139, 0.0029661, 0.000804986, 0.000914697, 0.000287989, 0.000264725, 0.000630987, 0.000351136, 0.000733556, 2.37422e-05, 0.000517571, 4.77645e-05, 0.0003319, 0.000405211, 0.000283034, 0.00159746, 0.00177935, 5.88713e-05, 7.11461e-05, 5.91759e-05, 0.000503971, 8.6978e-06, 0.00120087, 0.000813549, 0.000343971, 0.000687556, 0.000340041, 0.000355475, 0.000275208, 0.000696902, 8.2474e-05, 7.0166e-05, 0.000210933, 3.90627e-05, 1.89524e-05, 0.000368501, 0.000351007, 0.00060404, 0.000106084, 0.000331506, 8.32554e-05, 2.15891e-05, 0.000193192, 6.84955e-06, 0.000331523, 3.66652e-05, 1.12194e-05, 1.92205e-05, 2.50185e-05, 5.33112e-05, 1.32986e-05, 8.2467e-05, 5.92258e-05, 0.00283379, 2.84016e-05, 5.96508e-05, 0.000182353, 0.000241587, 4.9405e-05, 0.000256186, 6.64698e-06, 4.18161e-05, 4.28466e-05, 1.34901e-05, 1.77119e-05, 1.13135e-05, 1.55582e-05, 1.01246e-05, 2.15158e-06, 4.84249e-06, 0.000493645, 5.86358e-05, 3.79742e-05, 0.000325346, 0.000130454, 5.81232e-05, 6.95239e-05, 0.000220773, 5.40073e-05, 0.000293129, 9.38502e-05, 3.10892e-05, 0.000135327, 0.000216272, 0.000535513, 0.000271741, 0.000101224, 7.6724e-05, 0.000640805, 0.000273377, 0.00658155, 0.000621818, 0.00113372, 0.000305679, 3.32551e-05, 2.73262e-05, 0.000193103, 0.000359639, 0.00254883, 0.000114939, 5.88418e-05, 8.44255e-05, 8.56286e-05, 0.00014276, 9.35917e-05, 0.000153505, 6.59843e-05, 0.000114629, 0.000208262, 4.78108e-05, 3.78621e-05, 2.35319e-05, 1.77599e-05, 1.24966e-05, 6.25085e-05, 0.000493294, 9.21813e-05, 5.45179e-05, 4.59296e-05, 4.71062e-06, 7.51103e-05, 0.000342672, 0.000133267, 3.98604e-05, 0.000152585, 7.34054e-05, 0.000268039, 3.68195e-05, 0.000300117, 6.67261e-05, 3.72905e-05, 1.53376e-05, 5.58414e-05, 3.97029e-05, 0.00076366, 6.3162e-06, 1.66135e-05, 0.000325842, 0.000388785, 7.0573e-06, 1.1251e-05, 0.000252132, 0.000904097, 5.66675e-05, 5.92502e-05, 0.000555106, 3.88775e-05, 4.98873e-05, 0.000385996, 3.6577e-05, 0.000351803, 0.000243754, 0.0002531, 0.00544143, 0.000563464, 0.00103145, 0.00181722, 0.000466597, 9.86767e-06, 0.000341469, 0.0015705, 0.000206699, 7.92459e-05, 0.000345893, 0.000593111, 3.06207e-05, 8.75688e-05, 3.0376e-05, 0.00197111, 1.03584e-05, 0.000251229, 0.000428186, 2.60413e-05, 1.62735e-05, 0.000118388, 0.000196341, 0.000216079, 0.000269054, 9.98427e-05, 7.99718e-05, 0.000267069, 2.71418e-06, 1.367e-05, 0.000239831, 0.000567004, 0.000292397, 0.000207042, 0.000774038, 0.000156459, 0.00022772, 0.000720916, 0.00062859, 0.00150372, 0.000512822, 0.000119685, 3.59513e-05, 1.95859e-05, 0.000101747, 6.89584e-05, 0.000450811, 0.000977539, 0.000681986, 6.28705e-05, 0.000568139, 0.000504166, 0.000472052, 0.000321606, 0.000639555, 9.82591e-05, 0.000272632, 2.82421e-05, 0.00426243, 0.000141967, 9.05156e-05, 0.000199219, 0.00452672, 3.99683e-05, 0.000368264, 0.0296196, 0.0004709, 7.90032e-05, 0.000176198, 0.000204283, 0.00739515, 1.6471e-05, 4.34332e-05, 0.000124695, 0.000136671, 2.44544e-05, 4.26264e-05, 8.98381e-05, 0.000859267, 0.00728019, 0.00102531, 0.000398277, 0.000125742, 5.01795e-05, 0.000100727, 1.86502e-05, 0.000123926, 0.000238162, 0.000418217, 0.000152855, 0.0018571, 0.000290411, 8.05784e-05, 0.000291634, 0.000174392, 0.000212666, 0.000200596, 6.78554e-05, 0.000258088, 1.28704e-05, 0.000396029, 8.78131e-05, 0.000241314, 0.000312604, 0.000107392, 0.00110502, 0.000166141, 0.000168017, 0.000474729, 6.27281e-05, 3.09645e-05, 2.28196e-05, 0.000283914, 0.00209177, 0.000227949, 0.000394017, 5.04358e-05, 0.00118297, 9.57517e-06, 0.000171489, 0.00078891, 4.71934e-05, 2.50277e-05, 0.000438497, 4.65839e-05, 3.58285e-05, 0.000123489, 0.0143988, 0.000553773, 0.000116806, 0.000316215, 0.000281624, 9.31204e-05, 0.000223639, 0.000307707, 0.000271174, 4.04945e-05, 3.46877e-05, 0.000431537, 0.00151671, 5.3359e-06, 7.53046e-05, 0.000235918, 2.56014e-05, 0.000112486, 0.0014654, 0.000480544, 0.000316342, 0.000544367, 0.000444946, 0.000607547, 0.000481535, 0.000104002, 3.70468e-05, 2.8174e-05, 0.00106104, 5.66228e-05, 0.000109865, 0.000220595, 0.000238312, 0.000102476, 0.000189638, 0.000149645, 0.00118128, 0.000259041, 0.00139206, 8.37191e-05, 0.000226957, 0.000306372, 0.000450006, 0.000106509, 0.000303578, 3.29038e-05, 0.000159814, 9.35702e-05, 0.000326935, 7.2301e-05, 5.81706e-05, 0.000159592, 0.000618135, 0.000134879, 0.000182668, 7.6661e-05, 0.00198678, 0.000125605, 0.000412593, 0.000615485, 4.16444e-05, 0.000203402, 0.00362499, 0.000604073, 0.000812219, 5.87236e-05, 0.000445537, 7.08274e-05, 0.000538608, 0.000330699, 1.94176e-05, 0.000821484, 0.0112315, 0.000932525, 4.65331e-05, 0.000127888, 0.000212339, 9.2608e-05, 0.000237337, 0.000529908, 3.73669e-05, 0.000106617, 0.00372401, 0.00353945, 0.000357516, 0.000152526, 0.000225924, 2.47649e-05, 5.32593e-05, 0.00123404, 0.000897687, 4.55089e-05, 0.000217386, 0.000406262, 3.08005e-05, 0.00455928, 0.000140245, 0.00053089, 0.000170293, 0.000447217, 0.000803549, 5.35199e-05, 4.1243e-05, 5.38527e-05, 0.00086276, 0.000112361, 0.000115841, 0.000209599, 8.73793e-05, 0.015637, 1.19475e-05, 1.12752e-05, 0.00139052, 0.00375697, 0.000647192, 6.92945e-05, 6.92814e-05, 0.00316057, 0.000166111, 0.00758618, 0.000948753, 0.000197994, 5.97069e-05, 0.000606452, 6.79047e-05, 5.83162e-05, 7.09706e-05, 0.00241091, 0.000311754, 8.73588e-05, 4.62264e-05, 7.9378e-05, 0.00141774, 0.00103621, 0.0379155, 0.0388486, 0.000159568, 9.80266e-05, 0.000818292, 0.000181057, 0.000274421, 0.000267676, 0.000226017, 5.04306e-05, 0.000275776, 0.000747819, 0.000149055, 0.0002615, 0.000160578, 8.37152e-05, 1.11533e-05, 9.96574e-05, 6.02464e-05, 0.000143476, 0.000239814, 0.000773004, 0.00441533, 0.000319619, 0.0006442, 5.03002e-05, 1.85892e-05, 8.72792e-06, 0.000120659, 0.00128101, 0.00125803, 7.76589e-05, 0.0045897, 0.000584449, 6.5189e-05, 0.0010469, 9.78898e-05, 1.77179e-05, 3.70118e-05, 5.48802e-06, 0.000212075, 0.000140139, 3.12479e-05, 0.000133606, 6.63303e-05, 0.00745455, 0.000123391, 0.00053998, 0.000146543, 4.19799e-05, 0.000315619, 4.18607e-05, 8.34441e-05, 0.000146504, 0.000260877, 6.31252e-06, 8.27619e-05, 0.00193441, 3.89437e-05, 0.000232354, 0.00077451, 4.3871e-05, 0.000841214, 2.80011e-05, 0.0023251, 0.000533739, 5.10349e-05, 7.1796e-05, 2.07323e-05, 0.000167656, 0.000143678, 0.00014051, 0.000108807, 0.000233951, 0.000541688, 0.000581492, 0.000194971, 6.06314e-05, 7.98194e-05, 0.000168223, 0.000221727, 4.17056e-05, 0.000429627, 0.000114364, 0.000403723, 0.000223281, 0.00434498, 0.000408545, 9.39891e-06, 0.000177339, 0.000601626, 0.000167407, 0.000210305, 0.00486655, 0.000501063, 0.000437227, 0.000286798, 5.71393e-05, 6.61158e-05, 8.59685e-05, 3.14945e-05, 0.000633855, 0.00105124, 0.000168122, 0.000471016, 0.000197255, 0.000241811, 2.63944e-05, 0.000267025, 9.11511e-05, 0.000679203, 0.0557757, 0.00381419, 0.000337966, 0.00482909, 5.72956e-05, 0.000130052, 2.01935e-05, 6.77933e-05, 4.27964e-05, 0.00175907, 0.000649202, 0.000907705, 0.00146462, 0.0166741, 0.0075755, 0.0245835, 4.10026e-06, 0.000940845, 0.000576762, 0.000410642, 0.0242613, 0.00392809, 1.0285e-05, 0.000278193, 8.99299e-05, 0.000228249, 9.60876e-05, 6.41274e-06, 1.52377e-05, 0.000132558, 0.000359305, 0.000768309, 0.000233007, 0.00724834, 4.80489e-05, 3.41695e-05, 0.00499713, 1.23432e-05, 3.43374e-05, 4.30402e-05, 5.59372e-05, 2.3765e-05, 0.00300365, 0.00431638, 0.000161293, 2.90676e-05, 0.000763955, 0.000167918, 0.000183525, 0.000683445, 0.00106749, 2.89752e-05, 0.000364255, 2.42001e-05, 0.00205144, 0.00235832, 0.00116137, 0.00301419, 0.000934434, 0.000845667, 7.64749e-05, 0.000820362, 0.000122261, 9.32608e-05, 0.000223282, 7.85736e-05, 2.60183e-05, 5.02393e-05, 2.43877e-05, 0.0105237, 0.000292717, 0.00139134, 0.00010222, 0.000164221, 3.3359e-05, 6.15358e-05, 8.36216e-05, 0.000747535, 4.55981e-06, 0.000331875, 0.00021054, 0.000624574, 0.000307265, 6.56963e-06, 0.00236067, 0.00044436, 0.000450789, 0.000353041, 0.000100967, 3.37242e-05, 0.00720478, 0.000148686, 3.37161e-05, 0.000218007, 0.000307866, 0.000852728, 1.23465e-05, 0.000321398, 0.00219071, 0.000254209, 0.000577832, 0.000126005, 0.000151009, 9.99839e-05, 0.000903734, 1.52066e-05, 0.000521119, 5.78883e-05, 1.16647e-05, 8.59855e-05, 9.17357e-05, 0.000196617, 1.47663e-05, 4.3448e-05, 0.000330379, 8.82993e-05, 0.00075393, 0.00018865, 2.31207e-05, 4.4101e-05, 0.00244245, 0.000283023, 0.0106583, 0.000117731, 2.23972e-05, 0.000153825, 0.000967888, 0.0001583, 2.60177e-05, 0.000290019, 0.00297588, 0.000398894, 1.71261e-05, 0.00858669, 0.00211729, 1.6242e-05, 0.000301345, 0.00137275, 0.000372956, 3.09104e-05, 0.000351122, 4.80173e-05, 1.63996e-05, 4.22602e-05, 3.19259e-06, 3.63146e-05, 4.45177e-05, 7.56894e-05, 5.70432e-05, 0.000136668, 0.000120945, 7.85435e-05, 4.80372e-05, 9.4714e-06, 0.000304405, 1.76088e-05, 3.26987e-05, 0.00010463, 5.63494e-05, 3.0221e-05, 2.3453e-05, 0.00012479, 8.95265e-06, 6.9025e-05, 1.08093e-05, 0.000699259, 0.000548753, 0.000196507, 0.00022843, 0.00051543, 0.000110283, 0.000173086, 0.000424951, 0.00035482, 1.48939e-05, 1.94998e-05, 0.000484427, 2.33123e-05, 4.86105e-05, 0.000148831, 4.71529e-05, 1.15327e-05, 1.46404e-05, 0.00138581, 9.02665e-05, 4.18611e-05, 0.000265539, 3.91982e-05, 0.0805373, 0.000834312, 0.00716731, 0.000183447, 0.00333792, 0.010186, 0.00180289, 0.00658991, 0.00702397, 0.0418009, 0.00235253, 0.00024491, 0.000449987, 9.01764e-05, 0.000527748, 0.000131736, 3.40312e-05, 0.00435704, 6.92113e-05, 0.000118533, 0.000149927, 4.5531e-05, 3.98839e-05, 5.29356e-06, 4.02724e-05, 2.74253e-05, 8.40502e-05, 6.12322e-05, 0.00344411, 0.000671335], 'classes': 971}]}

Related

Django restframework SerializerMethodField background work

I am writing a project in Django with rest framework by using SerializerMethodField. This method makes queries for every row to get data, or View collects all queries and send it to DB? Django can make it as one joint query?
class SubjectSerializer(serializers.ModelSerializer):
edu_plan = serializers.SerializerMethodField(read_only=True)
academic_year_semestr = serializers.SerializerMethodField(read_only=True)
edu_form = serializers.SerializerMethodField(read_only=True)
def get_edu_plan(self, cse):
return cse.curriculum_subject.curriculum.edu_plan.name
def get_academic_year_semestr(self, cse):
semester = cse.curriculum_subject.curriculum.semester
return {'academic_year': semester.academic_year, 'semester': semester}
def get_edu_form(self, cse):
return cse.curriculum_subject.curriculum.edu_form.name
class Meta:
model = CurriculumSubjectEmployee
fields = [
'id',
'name',
'edu_plan',
'academic_year_semestr',
'edu_form'
]
class SubjectViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = SubjectSerializer
def get_queryset(self):
contract = self.request.user.employee.contract
if contract is None:
raise NotFound(detail="Contract not found", code=404)
department = contract.department
cses = CurriculumSubjectEmployee\
.objects\
.filter(curriculum_subject__department=department)
return cses

Flask appbuilder how to use existing model to create a new model and save into DB

Basically I have model A and B .
class A(ModelView):
datamodel = SQLAInterface(Tabel_A)
and
class B(ModelView):
datamodel = SQLAInterface(Table_B)
A has a action like :
#action("mularchive", "Archieve", "Archieve all Really?", "fa-rocket", single=False)
def mularchive(self, items):
self.update_redirect()
for item in items:
new_item = item.__class__()
new_item.name = item.name
B.add(new_item)
return redirect(self.get_redirect())
So I'm expect object b will save into table B.
But this is not working and it complain about
AttributeError: 'Table A' object has no attribute 'method_permission_name'
please help, I'm new to flask appbuilder and python... not sure where I made mistake .
for item in items
b = B(name = item.name)
db.session.add(b)
db.session.commit()
self.update_redirect()
return redirect(self.get_redirect())

How to extend search record to also look at custom Many2Many field(s)?

In this image, there is a product search record that will search for name and default_code. I need to make it so that it will also look at my custom Many2Many field.
This is the field in the inherited model.
product_list = fields.Many2many("product.list", string="Product List")
The custom model only has _name, _description, and name variables.
The question is how to make the search to also look at all of the possible Many2Many data of this field.
I have tried this in the inherited model:
#api.model
def name_search(self, name='', args=None, operator='ilike', limit=100):
res = super(product_template_inherit, self).name_search(name='', args=None, operator='ilike', limit=100)
ids = self.search(args + [(name, 'in', 'product_list.name')], limit=limit)
if ids:
return ids.name_get()
return res
Nothing happens to the search. It still searches using the same behavior regardless of the code above.
Summary: I need to be able to search product by product list (custom Many2Many field inherited in the product.template model)
=============================================
UPDATE
Current code from what I have been trying is now this.
#api.model
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None):
args = args or []
if operator == 'ilike' and not (name or '').strip():
domain = []
else:
domain = ['|', ('name', 'ilike', name), ('product_list.name', 'ilike', name)]
product_ids = self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
return self.browse(product_ids).name_get()
However, it looks like it still searches using the same old fields. It does not change to behave as my function is written.
You can compute the search domain then return the result of the _search method.
The fleet module already uses the same logic to search vehicles using the driver name, you have just to replace the driver_id with product_list:
class ProductProduct(models.Model):
_inherit = 'product.product'
#api.model
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None):
args = args or []
if operator == 'ilike' and not (name or '').strip():
domain = []
else:
domain = ['|', ('name', operator, name), ('product_list.name', operator, name)]
return self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)

What does the path /shop/get_suggest mean?

I found a module that autocomplete search in a Website e-commerce with high-light match words and image. But I did not really understand what each command do .
Can you please explain to me how this code work, and why they did /shop/get_suggest?
class WebsiteSale(http.Controller):
#http.route(['/shop/get_suggest'], type='http', auth="public", methods=['GET'], website=True)
def get_suggest_json(self, **kw):
query = kw.get('query')
names = query.split(' ')
domain = ['|' for k in range(len(names) - 1)] + [('name', 'ilike', name) for name in names]
products = request.env['product.template'].search(domain, limit=15)
products = sorted(products, key=lambda x: SequenceMatcher(None, query.lower(), x.name.lower()).ratio(),
reverse=True)
results = []
for product in products:
results.append({'value': product.name, 'data': {'id': product.id, 'after_selected': product.name}})
return json.dumps({
'query': 'Unit',
'suggestions': results
})
This controller function will be activated when you load page your_domain/shop/get_suggest.
The function just searches for products with similar name of the query given in the search.
Please go through this documentation to learn basics of building a website

Using SQS with related data (Haystack)

I having two fields for a Paragraph Model, with one of them being a ManyToMany field.
class Tag(models.Model):
tag = models.CharField(max_length=500)
def __unicode__(self):
return self.tag
admin.site.register(Tag)
class Paragraph(models.Model):
article = models.ForeignKey(Article)
text = models.TextField()
tags = models.ManyToManyField(Tag)
def __unicode__(self):
return "Headline: " + self.article.headline + " Tags: " + ', '.join([t.tag for t in self.tags.all()])
admin.site.register(Paragraph)
And my .txt files reflects the ManyToMany relationship to index tags-
{{object.text}}
{% for tag in object.tags.all %}
{{tag.tag}}
{% endfor %}
My views.py then uses SQS to search for all the tags (I want to accomplish this first before including text field) and retrieves those. So in this case, the query is "Politics"-
def politics(request):
paragraphs = []
sqs = SearchQuerySet().filter(tag="Politics")
paragraphs = [a.object for a in sqs[0:10]]
return render_to_response("search/home_politics.html",{"paragraphs":paragraphs},context_instance=RequestContext(request))
Edited:
and my search_indexes.py
class ParagraphIndex(indexes.SearchIndex, indexes.Indexable):
text= indexes.CharField(document=True, use_template=True)
tags= indexes.CharField(model_attr='tags')
def get_model(self):
return Paragraph
def index_queryset(self):
return self.get_model().objects
def load_all_queryset(self):
# Pull all objects related to the Paragraph in search results.
return Paragraph.objects.all().select_related()
However this doesn't retrive anything even though a few paragraphs have tags that are "Politics". Am I missing anything here or should I approach related data another way? I am a beginner with Haystack so any help will be much appreciated. Thanks in advance!
So this is a very useful article that helped me solve the problem.
Based on the article, this is how my search_indexes.py looks now:
class ParagraphIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
tags = indexes.MultiValueField()
def prepare_tags(self,object):
return [tag.tag for tag in object.tags.all()]
def get_model(self):
return Paragraph
def index_queryset(self):
return self.get_model().objects
def load_all_queryset(self):
# Pull all objects related to Paragraph in search results.
return Paragraph.objects.all().select_related()
and my views.py:
def politics(request):
paragraphs = []
sqs = SearchQuerySet().filter(tags='Politics')
paragraphs = [a.object for a in sqs[0:10]]
return render_to_response("search/home.html",
{"paragraphs":paragraphs},
context_instance=RequestContext(request))
And I am using elasticsearch for the engine. Hope this helps!