Skip to Content
Menu
This question has been flagged
1 Reply
2267 Views

I made a simple model called "Movie", and defined a function to create a new record


class Movie(models.Model):    _name = 'cinema.movie'​
    name = fields.Char(string='Name',required=True)
    category = fields.Char(string='Category',required=True)
def create_movie(self):
        movie1 = {
                 'name': 'Movie A',
         'category': 'Action film'
                 }
                 self.env['cinema.movie'].create(movie1)


Then I add a button in the tree view of the model. When I create the first record, a button appears after the field of category, and it works when I click on it

<button string="Create Movie" type="object" name="create_movie"/>



However, when I try to trigger the code by a menu item, it doesn't work

<record id="action_create_movie" model="ir.actions.server">
<field name="name">Create Movie</field>
<field name="type">ir.actions.server</field>
<field name="model_id" ref="model_cinema_movie"/>
<field name="state">code</field>
<field name="code">model.create_movie</field>
</record>


I'm very new to python and odoo. I sort of feel it might be resulted by the difference of a class and instance, but I really don't know how to fix this. Can anybody help me? Maybe just give  some keywords and I could google it. 

Thanks.




Avatar
Discard
Author

Hi Ravi, I don't know why but it seems I cannot reply to your answer.

I tried to add "()" before, but it still doesn't work. But after looking up answers for few hours, I finally found a way to fix this. What other people suggest to do is like this:

<field name="code">env.get('cinema.movie').create_movie()</field>

Currently, I still don't understand the mechanism behind it, and It would be great if someone could explain why it happens.

Best Answer

it seems you forgot to call create_movie method in server action :)

<field name="code">model.create_movie()</field>​

Avatar
Discard