I have a field of type many2one. Also in my database I have many records.
So right now I want to change the type of this field from many2one to many2many, but when I create a record it shows me an Integrity Error.
This is my field:
bed_id = fields.Many2one('i.bed', 'bed', required=True, ondelete="restrict", track_visibility="onchange")
And I was change it to this:
bed_id = fields.Many2many('i.bed', 'bed', required=True,string="beds", track_visibility="onchange")
The Integrity Error is:
The operation cannot be completed, probably due to the following:
- deletion: you may be trying to delete a record while other records still reference it
- creation/update: a mandatory field is not correctly set
[object with reference: bed_id - bed.id]
How can I fix that without deleting the existing records in my database?
I would change the name of the new (many2many) field to bed_ids.
Firstly it is for the sake of Odoo guidelines and secondly i don't think Odoo can migrate a field from many2one to many2many, because on database side it is a complete other relation. Many2one fields are just foreign keys to other tables, but many2many fields are realised by an associative table.
That's why i would let the old field just exist, so you can migrate it yourself. After migration you should remove the field or let Odoo remove the field, by deleting it in your code.
Related
I have a model with a CharField that has a choices set, but I need to change the field to a ForeignKey field. I have made the changes but, when I make the migration, I got an error saying django.db.utils.DataError: invalid input syntax for integer: "GRAND_TOURISM", that I realize means that the current content of the field doesn't match the new foreign reference constraint.
Is there any way I can instruct Django to change current values to Null when migrating?
I am new to Django.
I was trying to design a model in django. First I did with adding some fields, then I migrated the code. Later I found that I should have some other fields. I added some new fields, let say a CharField. Then while I was doing the migration, its showing error like you are trying to add a non-nullable field without a default. Can anybody tell should I add every time a default value to a new field OR Is there any other way to handle this?
You would need to add null=True to the field parameter that gives you this error in your model to allow for null values or give it a default value default=<value> and then re-run your migration
I'm working on a payment processor (in django 1.7) and I'm having trouble with these 2 models: Transaction and Event. They are in a custom many2one relationship in the sense that the field reference is unique in Transaction and Event entries having the same reference field belong to the Transaction with that reference. I was wondering if there a clean way to represent this in django.
I removed the reference field from the Event model, and added this transaction field:
transaction = models.ForeignKey('Transaction',db_column='reference',
to_field='reference',db_constraint=False, related_query_name='events')
This way I was able to access the transaction object from the event and use it nicely in queries, but now I can't access the reference field form an Event instance. This is weird since with a normal ForeignKey you're able to use both object and object_id.
Also I had to fake out the generated migration:(.
Is there a clean way to this?
I am trying to figure out the best way to save a model that I've got using the django orm. I have a model/table, User. Additionally, I have a model/table called ContactInfo, where we store a foreign key to the User table.
I understand that common django orm practice would be to put the foreign key for the ContactInfo model into the User model, but at this point, we do not want to add anything to the already monolithic user table, so we put the foreign key into the ContactInfo model.
I understand that I can store the User model in the ContactInfo model, call save on ContactInfo, and it should save the User model, but what if I have a one-to-many relationship with users and their contact info? I would rather not have multiple instances of the user table within (1-many) instances of the contact info model/object.
If I can clear anything up, please let me know. At the current moment, the best idea I have is to store an instance of the ContactInfo list as user.contact_info, and override the save method for user user.save() to check for contact_info, and if it exists insert the user.id into each model and save. Unfortunately I just feel like this is a bit messy, but being new-er to django and python, I'm not sure what my options are.
Any help would be greatly appreciated, thanks!
I am not sure if I understand your question correctly. Django provides well support for 1-N relationship. If ContactInfo has a foreign key of User, by default, it's a 1-N mapping.
ContactInfo ---------> User
N 1
So, there is only one User record in your database, looks like this
Table User Table ContactInfo
---------------------------------------------
id user_name id user_id
1 someone 1 1
2 1
3 1
And you don't need to override save method. When you need to add a Contact,
contact = ContactInfo(user=target_user)
# other stuff
contact.save()
#or
target_user.contactinfo_set.create(...)#contactinfo_set is the related name of target_user
#Django maintains the foreign key things.
If you use methods above to insert a new ContactInfo record, then you do not need to iterate your contact_info list to insert user.id into the database.
I am not sure if you're meaning a custom User model or the standard model that ships with Django. If the latter, then Django provides a standard way of storing additional information, called user profiles, associated with each user. See this section in the documentation for details.
How can I display fields of two record in the same tree view, Knowing that these tables are linked with OneToMany relashionship ?
here is my field:
'class_id': fields.many2one('mod.class', 'Entree', required=True),
In my tree view :
<field colspan="4" name="class_id" nolabel="1" widget="one2many_list"/>
But this can just display foreign key but i need other fields
I'll appreciate any help .Thank you
Usually, I use a related field to display fields from a related table.
Make related field or make a view(database) without table like many reports. Or make dashboard type view . It is upto you and your requirement.
In OpenERP's addons folder goto addons > base > partner > partner.py, there you can see a field named 'address' which is a one2many field. Also you can find some related fields like 'mobile','phone','city' etc. which are related to the address field. These related fields can be used in the tree view.
This is the same thing that you are trying to do.