This question has been flagged
5 Replies
10892 Views

Hi, 

I want to record my custom form in my database but don't succeed

controllers.py :

@http.route('/coopaname_helpdesk/post_form_ticket/<string:model_name>',  methods=['POST'], type = 'http', auth='public',  website= True )
def website_form(self, model_name, **kwargs):

try:
data = { 'test' : 'success'}

cnx = psycopg2.connect('dbname=<coopaname-2019-06-03>', 'user=<admin>')
mark = cnx.cursor()
query = ("""
INSERT INTO <monmarchepublic> ('x_cat','x_objet_consultation','x_membre_interne','x_membre_externe','x_equipeok','x_admin','x_type_marche','x_ville_adju','x_date_order','x_date_limite','x_duree_marche','x_montant_marche','x_num_ref','x_pj','x_name','x_mail','x_inputstreet1','x_inputcity1','x_inputzip1','x_inputstreet2','x_inputcity2','x_inputzip2')
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,);

"""
)
mark.execute(query)

return json.dumps(data)
except:
return json.dumps({'error_fields': " Tout cassé"})

But it's doesn't work, can you help me my friends please 

Avatar
Discard
Best Answer

thanks

Avatar
Discard
Best Answer

Hello,

If you want the data of your custom module to be inserted in the database you can do it easily with the "Store=True" settings

For example in your python file that describes the model you can do this:
Test = fields.Integer(string="hello", store=True)

The value will then be automatically stored in the database. If it is in an already existing template the column will be added, if it is a template you created the table will be generated.


i hope it help.

Avatar
Discard
Author Best Answer

Thanks for your help my friends but my request still sending in 200 with my values's form but nothing in Database


model.py : 


# -*- coding: utf-8 -*-

from odoo import models, fields, api


class MonHelpdeskTicket(models.Model):
"""Champs qui compose le modele d'une offres. Ce sont principalement les champs à compléter dans le formulaire"""

_name = "monmarchepublic"
_inherit = 'res.users'
_inherit = [
'mail.thread',
'ir.needaction_mixin',
]
x_name = fields.Char('Nom et Prenom', readonly=0, website_form_blacklisted=0, Store=True )
x_objet_consultation = fields.Char('Objet de la consultation', readonly=0, website_form_blacklisted=0, Store=True)
x_membre_interne = fields.Char("Membres de l'équipe INTERNE Coopaname", readonly=0, website_form_blacklisted=0, Store=True)
x_membre_externe = fields.Char(" Membres de l'équipe EXTERNE et/ou informations utiles", readonly=0, website_form_blacklisted=0, Store=True)
x_equipeok = fields.Boolean('Equipe complète', readonly=0, website_form_blacklisted=0, Store=True)
x_admin = fields.Char('Administration', readonly=0, website_form_blacklisted=0, Store=True)
x_inputstreet1 = fields.Char('N° et Rue', readonly=0, website_form_blacklisted=0, Store=True)
x_inputcity1 = fields.Char('Ville', readonly=0, website_form_blacklisted=0, Store=True)
x_inputzip1 = fields.Char('Code Postal', readonly=0, website_form_blacklisted=0, Store=True)
x_date_order = fields.Date('Date de publication', readonly=0, website_form_blacklisted=0, Store=True)
x_type_marche = fields.Selection([("offre_ferme", "Appel d'offre Fermé"), ('offre_ouvert', "Appel d'offre Ouvert"), ('concours', 'Concours'), ('consultation', 'Consultation'), ('mapa', 'MAPA')], readonly=0, website_form_blacklisted=0, Store=True)
x_date_limite = fields.Datetime('Date et heure limites de dépot de candidature', readonly=0, website_form_blacklisted=0, Store=True)
x_duree_marche = fields.Char('Durée du marché', readonly=0, website_form_blacklisted=0, Store=True)
x_montant_marche = fields.Char('Montant du marché', readonly=0, website_form_blacklisted=0, Store=True)
x_inputstreet2 = fields.Char('N° et Rue', readonly=0, website_form_blacklisted=0, Store=True)
x_inputcity2 = fields.Char('Ville', readonly=0, website_form_blacklisted=0, Store=True)
x_inputzip2 = fields.Char('Code Postal', readonly=0, website_form_blacklisted=0, Store=True)
x_num_ref = fields.Char('Numéro de référence attribué au marché', readonly=0, website_form_blacklisted=0, Store=True)
x_cat = fields.Selection([("Etudes, Maitrise d oeuvre, Controle", "Etudes, Maîtrise d'oeuvre, Contrôle"), ('Travaux de batiment', 'Travaux de bâtiment'), ('Travaux Publics', 'Travaux Publics'), ('Fournitures', 'Fournitures'), ('Services', 'Services')], readonly=0, website_form_blacklisted=0, Store=True)
x_pj = fields.Char('Pièce Jointe', readonly=0, website_form_blacklisted=0, Store=True)
x_ville_adju = fields.Char('Ville du Pouvoir Adjudicateur', readonly=0, website_form_blacklisted=0, Store=True)
x_mail = fields.Char('Email', readonly=0, website_form_blacklisted=0, Store=True)
x_etape = fields.Selection([('New', 'New'), ('CCTP', 'CCTP'), ('En cours de traitement', 'En cours de traitement'), ('Valide', 'Validé'), ('Refuse', 'Refusé'), ('Archive', 'Archivé')], readonly=0, website_form_blacklisted=0, Store=True)
message_ids = fields.One2many('mail.message', 'res_id', string="Commentaire", Store=True)
partner_ids = fields.Many2many('res.partner', string='Recipients', help="List of partners that will be added as follower of the current document.", readonly=0, website_form_blacklisted=0, Store=True)
message_partner_ids = fields.Many2many(
comodel_name='res.partner', string='Followers Test',
compute='_get_followers', search='_search_follower_partners', readonly=0, website_form_blacklisted=0, Store=True)
follower_ids = fields.One2many('mail.followers', 'res_id', string="follo", readonly=0, website_form_blacklisted=0, Store=True)

and 

controller.py : 

@http.route('/coopaname_helpdesk/post_form_ticket/<string:model_name>',  methods=['POST'], type = 'http', auth='public',  website= True )
def website_form(self, model_name, **kwargs):
try:
objet_consultation = kwargs.get('x_objet_consultation')
cat = kwargs.get('x_cat')
membre_interne = kwargs.get('x_membre_interne')
membre_externe = kwargs.get('x_membre_externe')
equipeok = kwargs.get('x_equipeok')
admin = kwargs.get('x_admin')
type_marche = kwargs.get('x_type_marche')
ville_adju = kwargs.get('x_ville_adju')
date_order = kwargs.get('x_date_order')
date_limite = kwargs.get('x_date_limite')
duree_marche = kwargs.get('x_duree_marche')
montant_marche = kwargs.get('x_montant_marche')
num_ref = kwargs.get('x_num_ref')
pj = kwargs.get('x_pj')
name = kwargs.get('x_name')
mail = kwargs.get('x_mail')
inputstreet1 = kwargs.get('x_inputstreet1')
inputcity1 = kwargs.get('x_inputcity1')
inputzip1 = kwargs.get('x_inputzip1')
inputstreet2 = kwargs.get('x_inputstreet2')
inputcity2 = kwargs.get('x_inputcity2')
inputzip2 = kwargs.get('x_inputzip2')


data = {
'x_cat' : cat,
'x_objet_consultation' : objet_consultation,
'x_membre_interne' : membre_interne,
'x_membre_externe' : membre_externe,
'x_equipeok' : equipeok,
'x_admin' : admin,
'x_type_marche' : type_marche,
'x_ville_adju':ville_adju,
'x_date_order': date_order,
'x_date_limite': date_limite,
'x_duree_marche': duree_marche,
'x_montant_marche' : montant_marche,
'x_num_ref' : num_ref,
'x_pj' : pj,
'x_name' : name,
'x_mail' : mail,
'x_inputstreet1' : inputstreet1,
'x_inputcity1' : inputcity1,
'x_inputzip1' : inputzip1,
'x_inputstreet2': inputstreet2,
'x_inputzip2' : inputzip2,
'x_inputcity2' : inputcity2
}

return request.env.cr['monmarchepublic'].create(data)

except:
return json.dumps({'error' : ' tout est casse'})

But the code run into the except because i receive the error in param



Avatar
Discard

You can create dictionary like this, data = {'val': variable1, 'val2': variable2}

Best Answer

Hi,

If you are looking to create/update record in the database, you can easily achieve it in odoo using the create and write method in the odoo, for this what you have to do is that, just call the corresponding methods of the models and pass the values as dictionary.


Suppose if you have to create a record in the table res.partner from the controller, you can do like this,


values = {'name': 'Odoo', 'street': 'Belgium'}
request.env['res.partner'].create(values)


Inside the dictionary values you can set all the values which has to be recorded in the table, and by calling the create method of the res.partner model pass this values.

Thanks

Avatar
Discard