How can I show specific number of records from api. That means if the model have 100 records i need to show only 50 records. How can I do that in api. please help me.
here is the code:
@http.route('/api/get-restaurant-list', type='json', methods=['POST', 'GET'], auth='public')
def get_restaurant_list(self, **kwargs):
restaurants = request.env['res.partner']
filter_domain = [('usertype', '=', 'restaurant')]
if 'filter_by' in kwargs:
filters = kwargs.get('filter_by')
for k in filters:
if k == 'available_food' or k == 'country_name' or k == 'state_name' or k == 'district_name':
if k == 'available_food':
filter_domain.append((k + '.name', 'in', filters.get(k)))
if k == 'country_name':
country_name = filters[k]
filter_domain.append(('country_id.name', '=', country_name))
if k == 'state_name':
state_name = filters[k]
filter_domain.append(('state_id.name', '=', state_name))
if k == 'district_name':
district_name = filters[k]
filter_domain.append(('district_id.name', '=', district_name))
else:
filter_domain.append((k, '=', kwargs.get('filter_by').get(k)))
if 'search' in kwargs:
filter_domain.append(('name', 'ilike', kwargs.get('search')))
restaurants = restaurants.sudo().search(filter_domain)
restaurant_list = []
for r in restaurants:
val = {
"id": r.id,
"name": r.name,
"email": r.email,
"mobile": r.mobile,
"street": r.street,
"street2": r.street2,
"zip": r.zip,
"city": r.city,
"state_id": r.state_id.id,
"state_name": r.state_id.name,
"country_id": r.country_id.id,
"country_name": r.country_id.name,
"district_id": r.district_id.id,
"district_name": r.district_id.name,
"opening_time": r.time,
# "opening_time": ('%.2f' % float(r.time)).replace('.', ':') if float(r.time) == int(
# float(r.time)) else ':'.join([str(r.time).split('.')[0], str(
# int((int('{:<02}'.format(str(r.time).split('.')[1])) * 60) / 100))]),
# "food_type_preferences": [{"id": f.id, "name": f.name} for f in r.food_type_preference_ids],
"available_food": [a.name for a in r.available_food],
"food_menus": [
{"id": fm.id, "name": fm.name, "price": fm.price, "description": fm.description}
for fm in r.menu_ids],
"location": {
"lat": r.partner_latitude,
"long": r.partner_longitude
},
"rating_avg": r.get_avg_rating(),
# "image": r.image_128,
"image": _generate_image_path(model=r._name, id=r.id,
field='image_1920')
}
restaurant_list.append(val)
data = {"success": True, "data": restaurant_list}
return data