Reputation: 13
We're trying to implement a custom reservation-type solution in our installation of Odoo 9. The solution includes a Reservation
model and a Session
model. One of the functions for a Reservation
is to operate as an associative container for Sessions
. The Session
model has a Many2one
field that links it to the Reservation
model.
At this point, we have the models defined and a standard form view that allows us to create a new reservation. However, we want the view to include a dropdown widget that allows us to select the number of accompanying sessions, and we want the reservation save functionality to create the appropriate number of sessions.
There are two parts we're struggling with in implementing this:
How can we include a widget (dropdown menu with numbers 1-8) that isn't actually tied to a model field? The value would be released as soon as the command mentioned in the next point is executed.
Is there a way to overwrite the standard save functionality to execute custom code (creating the number of Sessions corresponding to the value of the custom widget and prepopulating them with standard values), or do we have to create a completely custom action and hide the default save button on the form view? We have figured this out, as you can see in our code.
We don't need anyone to code this out for us, but since we're new to the platform pointing us in the general direction of appropriate methodologies would be great.
Edit:
Here is the relevant code we have so far:
models.py
class ResNumber(models.Model):
_name = 'restest.res_num'
name = fields.Char(string="Number to book", required=True)
num = fields.Integer(string="Number", required=True)
class Reservation(models.Model):
_name = 'restest.reservations'
name = fields.Char(string="Title", required=True)
num_booked = fields.Many2one('restest.res_num', ondelete='set null', string="Number to book")
slot_id = fields.Many2one('restest.slots', ondelete='set null', string="Slot ID")
session_ids = fields.One2many('restest.sessions', 'reservation_id', string="Session IDs")
customer = fields.Many2one('res.partner', ondelete='set null', string="Customer")
@api.model
def create(self, vals):
res_id = super(Reservation, self).create(vals)
numValue = res_id.num_booked.num
resIdValue = res_id.id
start = 1
while start <= numValue:
self.env['restest.sessions'].create({'name': start, 'reservation_id': resIdValue})
start = start + 1
return res_id
class Session(models.Model):
_name = 'restest.sessions'
name = fields.Char(string="Title", required=True)
reservation_id = fields.Many2one('restest.reservations', ondelete='set null', string ="Reservation ID")
views.xml
<record model="ir.ui.view" id="reservation_create_view">
<field name="name">reservation.form</field>
<field name="model">restest.reservations</field>
<field name="arch" type="xml">
<form string="Make a Reservation">
<sheet>
<group>
<field name="name"/>
<field name="slot_id"/>
<field name="num_booked"/>
</group>
</sheet>
</form>
</field>
</record>
<record model="ir.actions.act_window" id="make_res_action">
<field name="name">Make a Reservation</field>
<field name="res_model">restest.reservations</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="view_id" ref="reservation_create_view"/>
</record>
defaultData.xml
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<record id="first_cat" model="restest.res_num">
<field name="name">1</field>
<field name="num">1</field>
</record>
<record id="second_cat" model="restest.res_num">
<field name="name">2</field>
<field name="num">2</field>
</record>
<record id="third_cat" model="restest.res_num">
<field name="name">3</field>
<field name="num">3</field>
</record>
<record id="fourth_cat" model="restest.res_num">
<field name="name">4</field>
<field name="num">4</field>
</record>
</openerp>
So the test implementation here is pretty simple. We created the ResNumber
model and prefilled it with entries from 1-4. The Many2one
field in the Reservation
model is the current means by which the user can select how many sessions to reserve. We overrode the default create command to create the number of sessions based on the value of the ResNumber
field.
However, what we are wanting is to remove the ResNumber
model entirely. It's not a good database practice to store this number in the table since it can be derived through an alternate query. What we are wanting to do is include a field on the form view for the Reservation
model with the number selection and then have the value be discarded as soon as the Session
instances are created. Is there any way to do that?
Upvotes: 0
Views: 957
Reputation: 9624
use a selection field and set the store property to False
fields.Selection([(1, '1'),
(2, '2'),
(3, '3'),
(4, '4')], string='ResNumber' store=False)
it's pretty straight forward, in the case of '1'
is displayed in the front-end to the user, but on the back end the value returned is the integer 1
and when you set store to False you're telling odoo not to store the value in the db (it should probably be used just to display information to the user or form some sort of calculation or logic)
Upvotes: 1