Hello im new in programming with openerp ODOO , well my issue is where i can find the functions of inserting into odoo database , well i created a new field and i want to insert the data of this field into the db
It sounds like as you said, you are just getting started.
I would advise going through the following tutorial for starters.
You need to get an understanding of how Models and Views work in Odoo first and foremost.
Assuming you have added a new field to your model you will need to add this new field to a view for this model.
You will notice that if you have the appropriate permissions you will have an "Edit" and "Save" button (depending on state) on the top left of your views.
These buttons are mapped to functions which can be found on your model.
When you define your model you will notice it inherits models.Model which adds a lot of functionality that you will need for your model. This includes all CRUD operations. You can overide the default function if needed like so
The CREATE METHOD
#api.model
#api.returns('self', lambda rec: rec.id)
def create(self, vals):
# CUSTOM CODE BEFORE RECORD CREATION
rec = super(FocusType, self).create(vals)
# CUSTOM CODE AFTER RECORD CREATION
return rec
THE WRITE METHOD
#api.multi
def write(self, vals):
# CUSTOM CODE BEFORE RECORD WRITE
result = super(FocusType, self).write(vals)
# CUSTOM CODE BEFORE RECORD WRITE
return result
If you want to store field value in database then add store=True within your field in python file. Then Your value store into database.
Related
I have created several models using Django, for one of the models I would like to have a trigger that does not allow deletes. See the model code below.
#pgtrigger.register(
pgtrigger.Protect(name='protect_deletes',operation=pgtrigger.Delete)
)
class NoDelete(models.Model):
salary = models.PositiveIntegerField()
id = models.PositiveIntegerField(primary_key=True)
name = models.CharField(max_length=50)
The application also has an website that allows you to interact with the database, when deleting a model of this type a function in views.py is called.
def delete(request, id):
temp = NoDelete.objects.get(pk=id)
temp.delete()
So it seems like the trigger does not function as I want it to since this delete removes the row from the database. When trying to delete the tuple directly from the database however the trigger is fired. Any ideas on how to get around this issue? I have to use a trigger in this assignment.
I am trying to dynamically acquire my Django Models using apps.get_model(), and then call a method from said Model to properly save the data I am passing it.
temp_model = apps.get_model(app_label='app', model_name=model)()
temp_model.save_data(data)
So far, I am able to access the Model's that I am working with, as well as the method within each Object. What I am unsure of is how to properly save the data within the Object. I have been attempting to use self._meta.get_fields() to iterate over each field and set the field equal to the data I am passing it, but this is where I am getting caught up.
Here is how I am accessing a list of the Model's fields. This is working as expected.
def save_data(self, data):
fields = [field for field in self._meta.get_fields() if field.name not in ['id', ]]
What I am unsure of is how I can then set the fields data to the appropriate piece of data that I want to pass it, and then save the Object.
I am trying to use citus data (https://www.citusdata.com/) with Django.
Most everything is working so far, except trying to save a model that has already been saved:
NotSupportedError: modifying the partition value of rows is not allowed
This is because django always includes every single field in the update SQL, even if that field has not changed.
In Citus, you must pick a field to be your partitioning field, and then you cannot change it. So, when I'm saving an object, it doesn't like that the partition key is in the update statement, even if it didn't change.
I know that you can pass the update_fields keyword arg to the save method, but I'm wondering if I can somehow tell django to NEVER include a field when updating?
Django does not provide this functionality "out of the box". You could override the save method of your class to set all fields other than your partition field as the value for update_fields
def save(self, **kwargs):
kwargs.setdefault('update_fields', ['field1', 'field2'])
return super(Class, self).save(**kwargs)
A more dynamic option, if you do not want to update this method everytime you change the fields of your class, would be to use the Meta API to get all fields of the class and exclude your partition field
def save(self, **kwargs):
kwargs.setdefault(
'update_fields',
[f.name for f in self.__class__._meta.get_fields() if f.name != 'partition_field']
)
return super(Class, self).save(**kwargs)
There are several other methods by which Django will attempt to update your models. Maybe a base class that all your models inherit from that implement these methods would work
This is the first project that I've used flask/flask-admin on. We have an API, created using flask and are now working on an admin interface to it. One of the requirements is upon the creation of a record in TableA, a number of related records need to be created in TableB which includes the ROW_ID of the new TableA entry.
I've created a form for the entry of data in to TableA (which works fine), but don't know how to go about automatically adding data to TableB. Is the new (TableA) ROW_ID returned once the data has been committed to the table? Is there a better way to do this?
Any thoughts?
The docs don't have a great example of it, but you can override all of the CRUD operations for models, including creation. You can handle creating and saving the model yourself, at which you have the primary key, and can make any other queries and create any other models you need.
I cobbled this together out of our code, so hopefully it's complete enough. Let me know if any of it's confusing, or it doesn't answer your question.
http://flask-admin.readthedocs.org/en/latest/quickstart/#model-views
from your_models import Employee, Manatee
from flask.ext.admin import Admin
from flask.ext.admin.contrib.sqla import ModelView
class EmployeeAdminView(ModelView):
# this is called when a model is created
def create_model(self, form):
person = Employee() # create new Employee
form.populate_obj(person) # use WTForms to populate the model
# create a new manatee for this new employee
# everybody needs a manatee for business purposes
# this assumes there's a sqlalchemy relationship() between the two models
manatee = Manatee()
manatee.name = "Janet"
person.manatee = manatee
self.session.add(person)
self.session.commit()
# at this point person.id should be available, so you can use that here
# to make any other queries you need to
return True
admin = Admin(app)
admin.add_view(EmployeeAdminView(Employee, db.session))
I am in the process of running a Datamigration using Django and South. I have already added a new field to my model with a Schemamigration, and now I am in the process of populating the field for all the objects of that Model.
The problem is that when I call the save() method on my objects in the datamigration, it is automatically updating the modified field that is on each object and all the objects are ending up with the same modified date. I would like to be able to preserve the modified date from before the datamigration if possible.
Currently my datamigration looks like this:
class Migration(DataMigration):
def forwards(self, orm):
for w in orm.Writer.objects.all():
w.type = 'outside'
if w.managed_by != None:
if w.managed_by.writer != None:
if w.id == w.managed_by.writer.id:
w.type = 'client'
w.save()
Is there a way to only save the values in the type field, without updating the modified date?
You can update your object to only change the fields that you desire by using the update() method available for your model's queryset (see https://docs.djangoproject.com/en/1.4/topics/db/queries/#updating-multiple-objects-at-once for additional details).
Though the documentation references using this feature for multiple objects, you can filter the update query to only target a single object by restricting to the PK of the object you're working with:
orm.Writer.objects.filter(pk=w.pk).update(type='client')