I want to add new non existing entity in database from multi-select form element.
Scenario is that, I have a multi select option in my form where user can select multiple persons. when user insert a keyword in multi select, i send ajax request to server to fetch results, if no result is found then user has can add new preson with same multi select. My form has many-to-many relationship. Below is the ui of my form.
As you can see i select to persons in multi select. Aneeq and Tariq Najib. But aneeq is an existing entity in database and on the other hand Tariq Najib is a non existing entity (New entity).
What i want to do is when i submit form, first doctrine have to add Tariq Najib in the table and then associate Tariq Najib to the other entity. Below is the code of multi select element.
<option value="1" selected >aneeq</option>
<option value="" selected >Tariq Najib</option>
And how doctrine Hydrator will help me to achieve this problem.
The perfect example of my problem is stackoverflow's tags system. we can add new tag if the tag is not already exist in the database.
Related
I have a tag model, with ManyToMany field "parents" to tag model, to itself. There is also "allowed_users" field.
I need to guard a view in such a way, that the user won't see any tags in parents field, to which he is not allowed.
I try to modify queryset, removing the corresponding tags from parents. But when I change the instance, tag_instance.parents.set(my_new_list) it gets saved automatically so I'm altering the database and changing the real value of the instance.
So the general question is "how to guard my view in such a way, that object's ManyToMany field is filtered by custom logic".
Another question is "how to set manytomany field without altering database", as this would be a solution to the former one.
yes I use DRF
You can use the Prefetch object to filter related table when SQL queries are executed.
For instance:
Tag.objects.preftech_related(Prefetch("parents", queryset=Tag.objects.filter(allowed_users=current_user).distinct()))
This will prefetch "parents" (meaning no additionnal sql query will run when accessing my_tag.parent.all()) and filter parents to keep only those with the current user in allowed_user
Note: Tag.objects.filter(allowed_users=current_user) will duplicate tag entries for each user in allowed_user, ence the .distinct() to keep one of each
I have a django model form for which I allowed partial form save since its a long form. Now I want the user to come back and complete the form later.
There is one hindrance however that my database cannot accept duplicate entries with same primary key. So should I remove primary key in my database to solve this or is there another way to make it possible? Suggestions please.
Why not keep only one row per user for that form? Just add one boolean field to check whether the form is submitted or is a draft. Something like is_submitted=models.Boolean(default=False). Keep updating the row with the new values on each partial save (a separate button for Save or an AJAX call for auto save).
When the form is submitted, just mark the field is_submitted = True and perform whatever actions you want after that.
I have a model with a many-to-many field that will expand as the user adds more entries for the model. In the form template if I use the conventional {{ form.field }} I get a multiple choice select as is expected, but the problem that will quickly become apparent is when there are 10, 100, 1000 or more choices. What I'd like to provide in the form is a search field where the user can search through all available entries and via AJAX return entries matching their search criteria where they can then select the individual entry choices to be saved in the database.
I'm assuming I will have to manually render the multiple choice form field, but after hours of research I cannot find an example of this anywhere online. Is there an example that exists and I just haven't been able to find? How is one supposed to manually create a form with a multiple choice field in Django? Or am I going about this all wrong?
I have an existing table called empname in my postgres database
(Projectid,empid,name,Location) as
(1,101,Raj,India),
(2,201,David,USA)
So in the app console it will have like the following
1)Projectid=Textbox
2)Ops =(view,insert,Edit)-Dropdown
Case1:
So if i write project id as 1 and select View Result:It will display all the records for Projectid =1(Here 1 record)
Case2:
If i write projectid as 3 and select insert it will ask for all the inputs like empid,name,address and based on that it will update the table .
Case3:
If i write projectid as 2 and select edit.Then it will show all the field for that id and user can edit any column and can save which will update the records in backend for the existing table
If there is not data found for the respective project id then it will display no records found
Please help me on this as I am stuck up with models
Once you have your models created, the next task should be the form models. I can identify atleast 3 form classes that you will need to create. One to display the information(case 1), another to collect information(case 2) and the last class to edit the information. Wire up the form to the views and add the urls.
A good reference could be a django a user registration form since it will have all the three cases taken care of.http://www.tangowithdjango.com/book17/chapters/login.html
How can I set a ForeignKey to the TYPO3 FrontendUsers in the Extension Builder?
Do I have to set the param map to existing table? oder make a relation?
What I want to do:
I've got a Model (People) with its own fields and values. and now I want to have a new Releation between this Model and the TYPO3 FE Users
PeopleNr = fe_user with uid 123
Note: As stated in other question creating relations between tables/models doesn't require adding foreign keys
I'm not quite sure what do you need it for, but here's description of two most common cases (screenshot shows how to achieve each case with Extension Builder)
Creating relation to FrontendUser model / fe_user table
In TYPO3 ver. 6.2 fe_user has a model as well: \TYPO3\CMS\Extbase\Domain\Model\FrontendUser, you can just add this as common relation in Builder modeling tool (fig. 2 at screenshot) in the field \Fully\Qualified\Classname (you need to Show advanced fields to set it (fig. 1) use this FrontendUser and it will create proper SQL and TCA definitions.
Real extending FrontendUser (FU) model
On the other hand if your People* model just extends the fe_user table, because you want to reuse existing fe_user table for your people, you can also extend FrontendUser model for real (fig. 3) in that case you'll inherit all getters, setters from FU etc. and you won't need to write them yourself.
In such case your model will be a separate type of fe_user, the discriminator field is by default: tx_extbase_type and builder will add new type like Tx_YourExt_Employee
* note: For model names you should use singular form i.e Man instead of People