Skip to Content
Menu
This question has been flagged
2 Replies
269 Views

So I'm trying to use a text computed field in the contact form to get a compute a google maps using the street, city, state, and zip code.

Avatar
Discard
Best Answer

It is possible to use a computed field in Odoo to generate a Google Maps URL using the address fields from the contact form.

Here are the general steps to create a computed field that generates a Google Maps URL:

  1. In Odoo, go to the Contacts module and create a new computed field for the model "res.partner" (the default model for contacts) using the Developer Mode.

  2. Name the field "google_maps_url" and set its type to "char" or "text".

  3. In the "Compute" function for the field, write a Python function that takes the street, city, state, and zip code fields as input and generates a Google Maps URL using those values. Here is an example function:

pythonCopy code@api.depends('street', 'city', 'state_id', 'zip')
def _compute_google_maps_url(self):
    base_url = "https://www.google.com/maps/search/?api=1&query="
    address = f"{self.street}, {self.city}, {self.state_id.name} {self.zip}"
    url = base_url + urllib.parse.quote(address)
    self.google_maps_url = url

In this function, we first define the base URL for the Google Maps search API, which includes an API key and the "query" parameter for the address. We then construct the address string using the street, city, state, and zip fields from the contact form, and encode it using the urllib.parse.quote function to ensure that any special characters are properly escaped. Finally, we concatenate the base URL and the encoded address string to get the final Google Maps URL, which we store in the computed field.

  1. Save the computed field and make sure it is visible in the contact form view. When a user enters or updates the address fields in the contact form, the computed field will automatically generate the corresponding Google Maps URL.

Note that in order to use the Google Maps API, you will need to obtain an API key from Google and include it in the base URL for the API. You may also want to customize the URL parameters or the layout of the Google Maps page to better suit your needs.

 

Avatar
Discard
Best Answer

#Create a url field put the url you want to see in iframe.
iframe_url = fields.Char(string='Dashboard Url')
# create an HTML object which acctuly shows the Iframe inside odoo
iframe_field = fields.Html("Iframe Preview", sanitize=False)

# while creating or writing the object set the url in iframe object.
@api.model
def create(self, vals):
 vals['iframe_field'] = f'''


'''
return super(models.Model, self).create(vals)



Avatar
Discard