onetomany relation field in Openerp - python

I try to create a related field on OpenERP 6.0.1 . is it possible to define two different onetomany relationfor the same field name? What all changes i must do in the(.py file and XML Files).

No you cannot do that:
the field names are keys in a Python dictionary, in what you write the second invoice_line will overwrite the first one
this would mess up OpenERP's ORM anyway as it does not handle relations to different tables.
So you need two different columns, one relative to account.invoice.line and the other to account.service.line. If you really need a merged view, then you can add a function field which will return the union of the invoice and service lines found by the two previous fields. But I'm not sure the forms will be able to handle this.

Related

What is the best approach to design model fields that is like a list in Django?

Let's say I have a model called 'SysApp'. Each system has 5 documents. Each document has fields:
Title
URL to the file (external url)
Description
Rather than defining multiple fields like
title_1,
url_1,
description_1,
title_2,
url_2,
description_2
(Hardcoded approach)
is there a better way to handle this type of use case?
One way of doing is to create a model storing each document and then SysApp will reference each document using a ForeignKey. However I still have to create field like document_1, document_2 etc. Also it would be quite difficult for editors to manage when there are 100+ SysApp and 3-400+ documents.
Is it possible to manage these fields like a list or dictionary?
Thank you
I think the best way to organize your 'SysApp - documents' relationship, assuming that each document is related to only one sysapp, is to use ForeignKey, as you mentioned.
In that case you'll only have to create 2 models: the first one is SysApp with a name field and the second is Document with fields title, url to file, description and a foreignkey to SysApp. Now you can create documents and attach them to the sys you want. So you do not need to specify document_2, document_3 etc. fields.
If you need to attach one document to more than one sysapp use ManyToMany instead of ForeignKey.

Simple REST API not based on a particular predefined model

Well, I do my first steps with Django and Django REST framework. The problem I face is that all examples throughout the whole Internet are based on hard-coded models. But the whole concept of models frustrates me a little bit, because I'm used to deal with different data which comes from numerous sources (various relational databases and nosql - all that stuff). So, I do not want to stick to a particular model with a fixed number of predefined fields, but I want to specify them just at the moment when a user goes to a particular page of my app.
Let's say I have a table or a collection in one of my databases, which stores information about users - it has any kinds of fields (not just email, name and likewise - all those fields as in all those examples throughout the web). So when a user goes to /users/ I connect to my datebase, get my table, set my cursor and populate my resultant dictionary with all rows and all fields I need. And REST API does all the rest.
So, I need a "first-step" example wich starts from data, not from a model: you have a table "items" in your favorite database, when a user goes to /items/, he or she gets all data from that table. To make such simplistic api, you should do this and this... I need this kind of example.
I think the key is to use the models differently. If you use onetomany or foreignkey references in your model construction you can more dynamically link different types of data together, then access that from the parent object.
For example, for your user, you could create a basic user model and reference that in many other models such as interests, occupation, and have those models store very dynamic data.
When you have the root user model object, you can access it's foreign key objects by either iterating through the dictionary of fields returned by the object or accessing the foreign key references directly with model.reference_set.all()

Django - how to validate custom form for adding many similar entries at once

I have a model called "occurrence" which tracks occurrences of species at different sites. The model has 4 fields.
refID (foreign key to the reference source of data)
siteID (foreign key to site)
speciesID (foreign key to species)
abundance (integer)
I know I could create a model-form to add an entry. But modelforms would be tedious because, most of the time I want to enter data for dozens or hundreds of species with the same combination of siteID and refID. I have created my own data entry form in the template to select a refID and siteID, and use jQuery to add new lines for speciesID and abundance. Thus, I have a single refID + siteID combination, with many speciesIDs + abundance lines. Then, the idea is to iterate over all the added lines and save all the occurrences in the view.
The problem is that validation of this form is quite difficult, as I have to do everything "manually" in the view. This seems like it might be a common problem, so I wonder.....
Am I missing a pre-existing Django solution here?
Probably you're, as far as I understand your question.
have a look at Formsets
The simple solution to this problem turned out to be using a modelform to save a single instance at a time. I made the refID and siteID fields "sticky" by passing back the saved values for those fields to the reloaded form using the "initial" argument to the modelform. This way, I can use all the built in form validation.

Generate a multi-select option on a form using a junction table -> web2py

I have two tables with a many-to-many relationship between them. One table holds types of properties, the other holds regions.
For the sake of simplicity let's say it goes something like this:
_property_
id
name
description
_region_
id
name
_property-region_
id
property.id
region.id
I want to build a form to create new properties, and I want this form to contain a multi-select option allowing the user to specify which region/s the property is available in.
I understand that I can create a multi-select form using list:reference without actually creating a junction table, but I would be interested in learning how to do this without de-normalising the database.
its not denoramalising?
each region can have many properties and relation is held by region...
on the query level you just ask for regions which hold the id of the property... (either way you have to query db for that even if you have junction/through table)
If you have specific info about relation, you have to process that logic yourself. Aint that hard,
create two forms validate property info and the record will be in database thus you posess the id. Then just add that id as one part of relation and all the selected items as other.

Django inline admin problems

I'm trying to do such a thing: I have two models, connected via Foreign Key, and every instance of first model needs to be linked with exactly 4 instances of second (I use InlineAdmin to create them at the same time using extra=4 and max_num=4).
There are two problems:
1 - I need each of four models to has it's own default read only value for one of it's fields, this value needs to be selected from the field's choices option. But I need them to be read only (I know about readonly_fields, but it's useless for that. Javascript don't seems to be a good solution...
2 - I need to specify default values for some field for all four models at the same time editing only one field. I'm thinking of two possible solutions: javascript or one additional, "fifth" model with all hidden fields except the one I need so I can override save() to use it's values for other models and delete it.
But which is the right way?
Try this documentation, it might help:
https://docs.djangoproject.com/en/1.5/ref/models/fields/#editable

Categories

Resources