how to display many2many relationship data in odoo - python-2.7

First Table
class vehicle_service(models.Model):
s_type=fields.Many2many('service.service','service_rel','sid','s_type',string="Service Name", required="1")
Second Table
class service_service(models.Model):
_name="service.service"
name=fields.Char("Name")
cost=fields.Float("Service Cost")

Related

How to order a query using `text` with count from another table

I'm trying to query a model and order it via the number of rows
of a joined table using SQL (via the text function)
The general syntax is:
model.query.filter(...).order_by(text(order_values))
This works when order_values is in the model I'm trying to query.
For example, order_values = 'created_on desc', where model
has a created_on field
I want to extend this to ordering based on the number of rows of a joined table.
For example, order_values = 'views desc', where views has a relationship with models:
class model(db.Model):
__tablename__ = 'models'
created_on = ...
views = db.relationship(
View,
backref='models',
passive_deletes=True
)
This gives me an error:
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.SyntaxError) syntax error at or near "FROM"
LINE 2: FROM videos ORDER BY model.views asc
How can I replace order_values to to achieve my goal?

How to do LEFT OUTER JOIN on Django ORM on non ForeignKey field

I have 2 models:
model A:
name = CharField()
...
model B:
title = CharField(null=True)
...
I want to get all record of model A which do not have record in model B by "name" = "title"
ON SQL I got this by:
SELECT "A".*
FROM "A"
LEFT OUTER JOIN "B"
ON "A"."name" = "B"."title"
WHERE "B"."title" IS NULL
How to write this using Django ORM?
You can use a simple exclude with a subquery of all titles from model B as following:
A.objects.exclude(name__in=B.objects.all().values('title))

programmatically set columns of a tables when populating from a list

I am going to populate a django-tables2 list from a list and I am following the tutorial example:
import django_tables2 as tables
data = [
{'name': 'Bradley'},
{'name': 'Stevie'},
]
class NameTable(tables.Table):
name = tables.Column()
table = NameTable(data)
My problem is that I do not know which are the fields needed, so it would be nice for me to be able to specify them at runtime from a list of names. E.g.,:
needed_columns = ["name"]
class NameTable(tables.Table):
for field in needed_columns:
ADD_FIELD(field, tables.Column())
Is this possible in some way??
django-tables2's Table has a contructor argument extra_columns:
extra_columns (str, Column) – list of (name, column)-tuples
containing extra columns to add to the instance.
You could use that to dynamically add columns while instantiating a table:
class MyTable(tables.Table):
name = tables.Column()
table = MyTable(data, extra_columns=[
('country', tables.Column())
])
You can have a look at how that is implemented to to see how to add columns to a table class.

How to render multiple forms with different data in django view

I have an attendance model using which I want to mark attendance of a list of users for a particular day. But I can't figure out how should I build the view that allows me to render a html table of users with their corresponding attendance form in one column of table in the template.
My model for attendance:
class Attendance(models.Model):
ATTENDANCE_TYPES = (
('present', 'Present'),
('absent', 'Absent'),
)
date = models.DateField("Date")
author = models.ForeignKey(User,related_name='attendances_taken')
is_present = models.BooleanField(default='True')
student = models.ForeignKey(User,related_name='course_attendances')
classroom = models.ForeignKey(Classroom,related_name='student_attendance')

Django ManyToManyField not present in Sqlite3

I'm new to Django and I have some issues with a ManyToMany relationship.
I work on a blastn automatisation and here are my classes:
class Annotation(models.Model):
sequence = models.IntegerField()
annotation = models.TextField()
start = models.IntegerField()
end = models.IntegerField()
class Blast(models.Model):
sequence = models.ManyToManyField(Annotation, through="AnnotBlast")
expectValue = models.IntegerField()
class AnnotBlast(models.Model):
id_blast = models.ForeignKey(Blast, to_field="id")
id_annot = models.ForeignKey(Annotation, to_field="id")
class Hit(models.Model):
id_hit = models.ForeignKey(Blast, to_field="id")
length = models.IntegerField()
evalue = models.IntegerField()
start_seq = models.IntegerField()
end_seq = models.IntegerField()
In a view, I want to access to Annotation's data from the rest of the model via this many to many field and then apply filters based on a form. But when I do a syncdb , the "sequence" field of the Blast class disappear :
In Sqlite3 :
.schema myApp_blast
CREATE TABLE "myApp_blast" (
"id" integer not null primary key,
"expectValue" integer not null
);
So I can't load data in this table as I want. I don't understand why this field disappear during the syncdb. How can I do to link the first class to the others (and then be able to merge data in a template) ?
A ManyToManyField isn't itself a column in the database. It's represented only by an element in the joining table, which you have here defined explicitly as AnnotBlast (note that since you're not defining any extra fields on the relationship, you didn't actually need to define a through table - Django would have done it automatically if you hadn't).
So to add data to your models, you add data to the AnnotBlast table pointing at the relevant Blast and Annotation rows.
For a many-to-many relationship an intermediate join table is created, see documentation here: https://docs.djangoproject.com/en/1.2/ref/models/fields/#id1