Django adding data to models from another class - python

I have too models speakerCabinet and speakerElement. speakerCabinet has many-to-many relation to speakerElement.
I need to do some calculations on ideal speaker cabinet size that are using some of the speakerElement variables. I would like to contain these calculations to separate class because they are also used without the model.
Then comes the tricky part. My view gets the speakerCabinet instance and generates the speaker element info with:
{% for cab in speakerCabinet.SpeakerElement.all %}
Is there any simple way add that calculation data to my speaker element model?

Related

Dynamically create model from form builder in django

I have to create model from form builder. Form field as column and title as model.
is possible to implement this concept in Django?
Create dynamically model from selected field of form builder and that model register in admin.
i.g. I have a saree as product and that contain attribute color, prize, size and then add mobile as another product that contain attribute color, prize, ram, screen_size etc. here, color and prize common field and remaining attribute different so, create two different model in django
Heartily thank you in advance !

Odoo 10 - Counting values of linked records

In Odoo 10 I have created my own custom application (using the new studio feature), however I have run into an issue trying to compute data between records that belong to different views.
In the scenario I have two models (model A and model B), where records from model B are connect to records from model A via a many2one relational field. There is a field in Model B that counts a numerical value entered into it.
Ideally what I would like to achieve is have some form of Automated Action / Server Action, that loops through the records in Model A, then loops through related records in Model B adding together the values of the previously mentioned numerical value field and sets the value of a field in model A equal to the equated number, before continuing onto the next record.
For example sake say the field names are:
Model A = x_a
- Model A ID Field = x_id_field
- Target field for computed value = x_compute
Model B = x_b
- many2one field = x_a_id
- numerical field = x_value_field
I have attempted to use the automated actions to execute some basic Python code (because I thought this would be as simple as a nested loop) however all my attempts have been failures due to not being familiar with how to loop through records in odoo and how to access other models and their records (from python).
How would I go about accomplishing this?
Ideally what I would like to achieve is have some form of Automated
Action / Server Action, that loops through the records in Model A,
then loops through related records in Model B adding together the
values of the previously mentioned numerical value field and sets the
value of a field in model A equal to the equated number, before
continuing onto the next record.
Create an Automated Action with Related Document Model = model a
On the Actions tab create a Server Action:
model_b_records = self.env['model_b'].search([('many2one_field', '!=', False)])
for record in model_b_records:
record.many2one_field.target_field_for_computed_value = record.numerical_field
Save the Server Action and execute it.
The code should be self-explanatory, for any questions do not hesitate to ask and comment below.

3 way many to many relationship in Django

I have a Person model, a Semester model, and a Room model.
I'm trying to do a 3 way many to many relationship.
I've achieved this with an associative entity (a 4th model), which has 3 foreign keys (one for Person, one for Semester, and one for Room).
What is the Django-ish way of doing this using the built in ManyToManyField? Or is it inappropriate to use that with a 3 way m2m? I was looking at funneling 2 ManyToManyFields using the through parameter, but I wanted to see if anyone else had figured this out.
Thanks.

python ndb: what's the best way of implementing categories

I have an ndb.Model:
class Product(ndb.Model):
pass
it needs to be categorized in a category tree.
the categories are NOT dynamic, meaning they cannot change.
prefferably not implemented using a class Category(ndb.Model)
because having the categories in the datastore means that you rely on them being there and in the event of testing that means creating them over and over again.
What is the best way of implementing this based on the criteria above ?
Add the category to the Product class as a StringProperty (or an enumerated IntegerProperty if you need to be that space efficient). You can easily have a product belong to more than one category by making it repeated (repeated=True).

Applying machine learning to recommend items from an existing database

I've got an existing database full of objects (I'll use books as an example). When users login to a website I'd like to recommend books to them.
I can recommend books based on other people they follow etc but I'd like to be more accurate so I've collected a set of training data for each user.
The data is collected by repeatedly presenting each user with a book and asking them if they like the look of it or not.
The training data is stored in mongodb, the books are stored in a postgres database.
I've written code to predict wether or not a given user will like a given book based on their training data, but my question is this:
How should I apply the data / probability to query books in the postgres database?
Saving the probability a user likes a book for every user and every book would be inefficient.
Loading all of the books form the database and calculating the probability for each one would also be inefficient.
I've written code to predict wether or not a given user will like a given book based on their training data
What does this code look like? Ideally it's some kind of decision tree based on attributes of the book like genre, length, etc, and is technically called a classifier. A simple example:
if ( user.genres.contains(book.genre) ) {
if ( user.maxLength < book.length ) {
print "10% off, today only!"
}
}
print "how about some garden tools?"
Saving the probability a user likes a book for every user and every book would be inefficient.
True. Note that the above decision tree may be formulated as a database query:
SELECT * FROM Books WHERE Genre IN [user.genres] AND Length < [user.maxLength]
Which will give you all books that have the highest probability of being liked by the user, with respect to the training data.

Categories

Resources