How to make the form view opened from another form view read only in odoo - odoo-8

In my module i have two models, book and room. In room i will keep room details. In book i will keep the booking details of room. I have a many2one field in book model relating to room model. When i save the record of book, near to the many2one field a redirecting button will come. On clicking that, it will open the form view of room model. I want to make the second form view to be read only. How can i do that?. I tried to keep only read access for room model, then i can't able to save book model record. So how can i complete
class room(models.Model):
_name = 'room'
name = fields.Char('room name')
class book(models.Model):
_name = 'book'
name = fields.Char('booking person')
time = fields.Datetime('time')
room_name = fields.Many2one('room','room name')

Hello First take one boolean(like temp_bool) in room object,
now in book object, when you select room then write temp_bool field as True in selected room,
now based on this boolean you can give attrs in room object like attrs="{'readonly': [('temp_bool','=','True')]}"

Related

Odoo how domain attribute works?

I have a One2many relation in my odoo ' student .student' model that's it
result_ids = fields.One2many("schoolresults.detail", "student_id", "SchoolResults")
"schoolresults.detail" model has the following Many2One relational fields
student_id = fields.Many2one("student.student", "Student", ondelete="cascade")
subject_id = fields.Many2one("schoolresults.subject", "Subject")
The problem is when adding the result ids field its possible to add some subject twice to the same student and with a different scores .
I tried to make sql_constrait 'UNIQUE (student_id, subject_id)' on schoolresults.detail
Model but it raises an error .
I don’t want the subject to appear on the selction field if it is already selected.
I think the answer is arround domain attribute,
this image may cover my language faults.
enter image description here
you should return a domain on onchange of your one2many field result_ids.
eg:
ids_list = []
#api.onchnage('result_ids')
def onchange_result_ids(self):
for record in self.result_ids:
if record.subject_id:
ids_list.append(record.subject_id.id)
return {'domain': {'subject_id': [('id', 'not in', ids_list)]}}

How can I get a list of filtered values from a many2many field in odoo 11?

I have a model named "student" that had a many2many relation with a model named "authorized" and another model named "authorized" with a many2many relation with the model "student". In the other hand, I have a third model named "exit_control" that contains a many2one relation with the model "student".
I wanna a second field many2one in the model "exit_control" that shows only the authorizeds related with the student selected.
In other words: if student1 is related with authorized6 and authorized8, and the complete table of authorized had 20 authorizeds, and I select the student1 in the field student_id of the model exit_control, I wanna show a field many2one with the authorized6 and authorized8.
Student model had:
authorized_ids = fields.Many2many('aula10.authorized', string='Authorizeds')
Authorized model had:
authorized_for_ids = fields.Many2many('aula10.student', string='Can get this student:')
Exit_control model had:
student_id = fields.Many2one('aula10.student',string='Student')
Given_to_id = fields.Many2one('aula10.authorized', string='Given to:')
Given_to_id is the field that I wanna use for show the filtered authorizeds.
I tried to use the attribute domain in view and in model, but was impossible for me.
Can you help me, please?
In model definition?
Given_to_id = fields.Many2one('aula10.authorized', string='Given to:', domain=[('authorized_for_ids', 'in', [student_id ])])
eventaully you can use computed field with store option?

Mezzanine Forms Dropdown

I'm trying out Django/Mezzanine and if I have a custom user profile as such:
class UserProfile(models.Model):
user = models.OneToOneField("auth.User")
street_address1 = models.CharField(max_length=100)
street_address2 = models.CharField(max_length=100)
postalcode = models.CharField(max_length=10)
city = models.CharField(max_length=32)
country = models.CharField(max_length=2)
phone = models.CharField(max_length=15)
Mezzanine creates a sign up form at account/signup/ and I would like to modify the Country field to have a drop down list of countries from a table or xml file. The foreign key is a two character field.
How should go about doing this? Do I create a model form or try to extend the right template (tried looking at accounts\templates\account_form.html but don't think it is there?
I believe if you defined a "choices" arg for the field, it'll do just that:
https://docs.djangoproject.com/en/dev/ref/models/fields/#choices
A quick Google search will probably also reveal some pre-built packages for country lists.

Form helpers in case of Single Table Inheritance

I have to implemene Single Table Inheritance for a class Person who can be of type Teacher, Student,Outsider.
class Person < ActiveRecord::Base
end
class Teacher < Person
end
class Student < Person
end
class Outsider < Person
end
What changes do I need to make in the routes and the forms while register a new user. I have a column(string) "type" in the people table which can be implemented as a dropdown in the form to register a new user. Is there anything else that I need to do in the form so the user is registered as a particular type of Person? Do I need to make any changes in the routes too?
Since you use one form to create all types of Persons, then you should stick with one Controller as well so you don't need to add any additional routes.
The type attribute is not really something you should assign manually to an instance, it should be set automatically by choosing which type of model to create.
I don't know how it looks in your controller and views, but you can extract the type of model to create like this:
class_type = params[:type].constantize
#person = class_type.new
On the other hand, if the type attribute is nested in a form_for in your view, then the type attribute is probably send to the controller like params[:person][:type] in which case it should be removed from the :person hash before it is used to create the new instance. Perhaps something like this:
class_type = params[:person].delete(:type).constantize
#person = class_type.new(params[:person])
Except adding a dropdown list of type selection in the form, there's nothing more to do. You can create a user in the normal way, like:
#user = Person.new params[:user]
But the type attribute could not be mass assigned, so you have to assign it separately.
#user.type = sanitize_user_type params[:user][:type]
The method sanitize_user_type is used to validate user input value.
The route for creating new user doesn't need to change. Whether other routes need to change or not depend on your requirement. Actually you can add the routes for Teacher, Student, Outsider and relative controllers, so that you can build restful urls.

Grails: can remoteField update multiple fields?

Assume i have a book entity with an isbn field.
When entered a isbn number, i want 2 fields to be updated: title and author.
My controller looks like this:
def ajaxGetBook = {
def book = Book.findByIsbn(params.isbn)
if(book==null) book = new Book()
render book as JSON
}
So my call works, and i get a full JSON Book.
Now i would like to update 2 texfields by the update attribute
<g:remoteField action="ajaxGetBook" update="title" name="isbn" value="${bookInstance?.book?.isbn}" paramName="isbn"/>
Now the title field gets updated with the full book object, so that doesn't work.
Is it possible to update field title with only the JSON book.title?
Is it possible to update more fields at once?
I could render book.title as JSON but that works for only one field.
Thank you
Well, the g:remoteField tag is explicitly for one field, so you can't use it to update more than one. So, you're left with 2 easy choices:
Use 2 g:remoteField calls... this isn't a bad option, as they would happen in near parallel, as they are Async calls.
Roll your own Ajax. use g:remoteFunction instead, and have the JS function that you place in the "success" attribute take your book, and update the appropriate HTML fields.

Resources