开发者

Django-Nonrel ManyToOne model creation on the fly

I have 3 models: Company, Project and Contact with the respective relationships:

Company-Project (M2M) 
Project-Contact (M2M) 
Company-Contact (OneToMany)

I have this big form in which people use to create a company, and also it should let the user to add开发者_运维问答 projects & contacts on the fly, without submitting the form.

I managed to port Django Admin's dynamic model creation button (that little green plus) for M2M fields. So while a company is being created, user can click on the green plus near projects field, a popup will appear and let him create a project instance. As he clicks submit on popup, the popup is closed and original form's project box is refreshed with the new project data.

For OneToMany relationships, normally the user selects the "One" part of the relationship while creating a model that stands in the "Many" side using a combo-box and it is possible to do the same if you create a Contact model and then select a pre-created Company instance in the ContactForm.

The problem is, I'm trying to have a reverse field for this relationship in the Company creation form. So ideally there should be an empty contacts field (new company, no contacts yet) with a green plus on the right side and when the user clicks on it a new popup will open and let him create contacts.

The only solution I could come up with is to add a temp_uuid field to the Contact, so that when a new instance of CompanyForm is created, it gets assigned an uuid and then every time the user clicks on the green plus and creates a Contact, it will assign form's uuid to the Contact's temp_uuid field (We may call this a pseudo-foreign key, no?). So that after the creation I'll be able to populate the form's field with contacts for this company, even though the company hasn't been created yet. And when the form is submitted I'll assign Company's pk to the each contact's foreign key and save them.

I bet there's a better way to do this. Also, projects runs on App Engine and uses django-nonrel, so any use of JOIN's is out of question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜