exiting lot_id in stock.move.line, showing same value from compute field - python-3.x

I got some issue that lot_name is not showing same value from another compute field in same recordset, stock.move.line. below is my code:
class StockMoveLine(models.Model):
_name = 'stock.move.line'
_inherit = 'stock.move.line'
lotter = fields.Char(string="Supplier Lot", compute='_compute_partner')
def _compute_partner(self):
if not self.lotter:
partner_id = self.env['stock.picking'].search([('name','=',self.reference)]).partner_id.id
self.lotter = str(partner_id)
if self.lot_name == "":
self.lot_name = self.lotter
else:
self.lot_name = "blank"
the lot_name had been already existed in base module field. So I would like to show partner_id in lot_name field as well. now I only see it in my new compute fieldl. I tried using #api.onchange but it is only work when the textfield of lotter is lost focus. how can I do to show the same value on both lotter and lot_name fields if there is no value earlier?

Add store=True in your field.Because compute field is not stored in your database.
lotter = fields.Char(string="Supplier Lot", compute='_compute_partner', store=True)

Related

User input not reflected in database (xampp)

In my program, I am getting the user input from combobox and inserting it to database. All values except 'classname' is getting reflected.Its value is shown as zero in the database. I am able to print all the values to be inserted before performing the SQL query.
This the function I executed.
def insert_staff():
id = entry_staffid.get()
subject = subject_combobox.get()
class_name = class_combobox.get()
class_time = timings_combobox.get()
print(id,subject,class_name,class_time)
staff = mysql.connector.connect(host="localhost", user="root", password="", database="school_database")
cursor_variable = staff.cursor()
query = "INSERT INTO staff_schedule(staff_id, subject_allotted, class_name, time_allotted) VALUES ('"+id+"','"+subject+"','"+ class_name +"','"+class_time+"')"
cursor_variable.execute(query)
staff.commit()
staff.close()
Can anyone tell why the value of 'classname' alone is not getting reflected?
Any help is greatly appreciated.
Initially I put the data type of class_name as INT but I was actually using VARCHAR values. This is the reason why the database reflected zero.

Django Form Field Not Setting Value After Submitting

I'm trying to post data from a form that contains incomplete data (I set the missing data in the View class before saving) for the following model. But the form does not get submitted as it is invalid (it's missing the harvest_amount, but I set the value on the webpage before submitting.
class Harvest(models.Model):
harvest_amount = models.IntegerField(validators=[MinValueValidator(limit_value=0)])
harvest_date = models.DateField()
harvest_for_plant = models.ForeignKey(Plant, on_delete=models.CASCADE)
and my form
class HarvestCreationForm(forms.ModelForm):
class Meta:
model = Harvest
fields = [
'harvest_amount'
]
def is_valid(self):
//check if Id in the url contains a valid id for a plant
return True
In this case I forgot to migrate my changes to the model (where a field was deleted). When the form posted the data it would always hit the not null constraint since the deleted field was not being set.

How can I dynamically set a default value for a WTForms FormField?

Given the following two WTFforms:
class NestedForm(FlaskForm):
note = StringField('Note', validators=[DataRequired()])
some_id = StringField('Some ID', validators=[DataRequired()])
class Form(FlaskForm):
id = HiddenField('ID')
nested_forms = FieldList(
FormField(NestedForm),
min_entries=1,
)
How can I dynamically set the value of some_id? For instance, from within a Flask view, if a value for some_id hasn't been provided.
I have tried setting the value using the following:
form.nested_forms[0].some_id = "some_contextual_default_value"
form.nested_form[0].data['some_id'] = "some_contextual_default_value"
... which don't seem to do anything. The form validation continues to fail with an error saying that the required field (some_id) is missing.
Use the data attribute.
form.nested_forms[0].some_id.data = "some_contextual_default_value"

Web2Py list:reference table, Load and set data

Maybe I'm missing something absurd, I'm not seeing, but this is my first app to study web2py.
I am unable to enter the data in Table Movies, which has fields related to other tables.
The list is loaded, but it is not registered in Movies registration.
Under the codes and the results.
db.py
Movie = db.define_table('movies',
Field('title','string', label = 'Title'),
Field('date_release','integer', label = 'Date Release'),
Field('duraction','integer', label = 'Duraction'),
Field('category','string','list:reference categories', label = 'Category'),
Field('actor','list:reference actors', label = 'Actor'),
Field('director','list:reference directors', label = 'Diretor'),
)
Category = db.define_table('categories',
Field('title','string', label = 'Title'),
)
validators.py
Movie.title.requires = [IS_NOT_EMPTY(), IS_NOT_IN_DB(db, 'movies.title')]
Movie.category.requires = IS_IN_DB(db, 'categories.title')
Movie.director.requires = IS_IN_DB(db, 'directors.name')
Movie.actor.requires = IS_IN_DB(db, 'actors.name')
Movie.duraction.requires = IS_INT_IN_RANGE(0, 1000)
Category.title.requires = IS_NOT_EMPTY()
movie.py
def add():
form = SQLFORM(Movie)
if form.process().accepted:
response.flash = "Successful! New movie added!"
redirect(URL('add'))
elif form.errors:
response.flash = 'Error'
else:
response.flash = 'Form, set data'
return dict(form = form)
List Load another tables - ok:
The items of list not record in DB:
The widgets displayed in the form are based on the IS_IN_DB field validators you have specified, and there are three problems with the way you have coded them.
First, list:reference fields, like standard reference type fields, store the record IDs of the records they reference -- they do not store values of other fields within the referenced records. So, the second argument to the IS_IN_DB validator should always be the ID field (e.g., categories.id).
Second, although the field will store record IDs, you want the form widget to show some other more descriptive representation of each record, so you should specify the "label" argument of the IS_IN_DB validator (e.g., label='%(title)s').
Third, list:reference fields allow for multiple selections, so you must set the "multiple" argument of the IS_IN_DB validator to True. This will result in a multi-select widget in the form.
So, the resulting validator should look like this:
Movie.category.requires = IS_IN_DB(db, 'categories.id', label='%(title)s', multiple=True)
The above will allow multiple db.categories IDs to be selected, though the form widget will display category titles rather than the actual IDs.
Now, all of the above can be made much easier if you instead define the referenced tables before the db.movies table and specify a format argument for each table:
Category = db.define_table('categories',
Field('title','string', label = 'Title'),
format='%(title)s')
Movie = db.define_table('movies',
...,
Field('category', 'list:reference categories', label = 'Category'),
...)
With the above code, there is no need to explicitly specify the IS_IN_DB validator at all, as the db.movies.category field will automatically get a default validator exactly like the one specified above (the format attribute of the db.categories table is used as the label argument).
You might want to read the documentation on list:reference fields and the IS_IN_DB validator.
As an aside, you might consider specifying your field validators within the table definitions (via the requires argument to Field()), as this is more concise, keeps all schema-related details in one place, and eliminates the need to read and execute an additional model file on every request.

Removing a column from result set in Groovy Sql rows

I have a query where I need to retrieve a column which I need only temporarily because I need to pass it in the parameter for a where clause, how can I remove this column and its value from my result set after it served that purpose. Hopefully the code will show what I mean...
def empQuery = "select id, name, address from Employee"
def retObj = [:]
def sql = new Sql(datasource)
retObj = sql.rows(empQuery.toString())
retObj.each {
def addressQuery = "select street from Address where employe_id = ${it['id']}
// at this point I want to remove 'id:n' from the result set hash map aka 'it'
}
Because later on I am displaying that result set on a page for the user, and the ID field is not relevant.
So can you please show the Groovy code to remove a column and its value from the rows data structure returned from sql.rows?
from http://docs.groovy-lang.org/latest/html/api/groovy/sql/GroovyRowResult.html
It looks like you can do something line:
retObj.each { it.remove('id')}
However I haven't tried it....

Resources