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.
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- CRM
- e-Commerce
- Accounting
- Inventory
- PoS
- Project management
- MRP
This question has been flagged
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:
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.
Name the field "google_maps_url" and set its type to "char" or "text".
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.
- 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.
#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)
Enjoying the discussion? Don't just read, join in!
Create an account today to enjoy exclusive features and engage with our awesome community!
Sign up