Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
1766 Lượt xem

I have 2 models. main model and a another model for the relationship

class MyModel(models.Model):
    _name = 'my.model'
   
    name = field.Char(string='name')
    email = field.Char(string = 'email')
    country = fields.Many2one(comodel_name = 'country.name', string='country')


class CountryName(models.Model):
    _name = 'country.name'

    country = fields.Char(string='Country')


these are the my models. i want to add new countries to the country.name model's country field from the frontend form.but frontend form also has the other fields of the my.model (name , email)  How do it ? can anyone tell me about how should i write qweb template and the controller file's code snippet for this.


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,

To add new countries to the country.name model, you'll need to handle the form submission through a controller. Here's an example:

XML:

<?xml version="1.0" encoding="UTF-8"?>

<odoo>

    <template id="custom_form_template" name="Custom Form">

        <t t-call="website.layout">

            <div class="container">

                <form action="/submit/custom_form" method="post">

                    <input type="hidden" name="csrf_token" t-att-value="request.csrf_token()"/>

                    <div class="form-group">

                        <label for="name">Name</label>

                        <input type="text" class="form-control" id="name" name="name" required="required"/>

                    </div>

                    <div class="form-group">

                        <label for="email">Email</label>

                        <input type="email" class="form-control" id="email" name="email" required="required"/>

                    </div>

                    <div class="form-group">

                        <label for="country">Country</label>

                        <input type="text" class="form-control" id="country" name="country" required="required"/>

                    </div>

                    <button type="submit" class="btn btn-primary">Submit</button>

                </form>

            </div>

        </t>

    </template>

</odoo>


Python:


from odoo import http

from odoo.http import request


class CustomController(http.Controller):


    @http.route('/submit/custom_form', type='http', auth='public', website=True, csrf=True)

    def submit_custom_form(self, **post):

        # Check if the country already exists

        Country = request.env['country.name']

        existing_country = Country.sudo().search([('name', '=', post.get('country'))], limit=1)

        if not existing_country:

            # Create new country if it doesn't exist

            existing_country = Country.sudo().create({'name': post.get('country')})

        # Create new record in my.model

        MyModel = request.env['my.model']

        MyModel.sudo().create({

            'name': post.get('name'),

            'email': post.get('email'),

            'country_id': existing_country.id,

        })

        #Return to a template or render a response if you want


Hope it helps

Ảnh đại diện
Huỷ bỏ
Tác giả

Thanks Cybrosys. Now It's working Nicely.

Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 12 16
3080
4
thg 7 23
2111
1
thg 10 20
5977
0
thg 3 15
6041
1
thg 2 25
10259