Save the dynamically populated value on dropdown - django

I'm using wagtail CMS for Django, I want to add a dynamically populated value for a dropdown and save it on the Page model, this is my code:
class MyPage(Page):
domain = CharField(max_length=10, choices=MY_CHOICES)
subdomain = CharField(max_length=10, choices=[('', '------')]
I've got some frontend logic to populate dynamically the options for subdomain, but after I hit save I got: The page could not be created due to validation errors And in the subdomain field: Select a valid choice. [my value] is not one of the available choices.
I can't use ForeignKey to populate subdomain because it depends from an external API service that we're using.
I tried to use a custom field that inherits from CharField with no success, it looks it executes validate method only for the domain field.

If you use the choices argument, you have to predefine the list of possible values. Read the relevant part of the docs (last two paragraphs of that section).
You could omit the choices argument from the model field definition and only render a HTML select tag in the frontend (which is then filled with options dynamically, like you explained).
You could also look into changing the default widget of the CharField to a select tag, like this answer and this part of the docs show.

Related

Flask WTForms - option_widget for SelectMultipleField?

I have a field in my Flask WTForm which uses a SelectMultipleField. I'm using a frontend library which renders the choices fine from the db and adds them to the input field as tags when I select them (like the tags do on this website when creating a question!).
However the data is not being saved, form.field.data and request.form.getlist('field') etc all show None. If I add option_widget=widgets.CheckboxInput to the SelectMultipleField, I can select and save data.
So what I'm wondering is, do I need make a custom field or widget in order for the form to use the selected options as the form data (for example, instead of checking if the field has been checked, it checks if it's in the input field). Going a bit mad reading all the documentation so grateful for a hint in the right direction! Code below:
field = SelectMultipleField(
"fieldname",
validators=[Optional()],
widget=widgets.ListWidget(prefix_label=False),
# option_widget=CheckboxInput(),
)
please try this way
from flask_appbuilder.fieldwidgets import Select2ManyWidget
"users": QuerySelectMultipleField(
_("group.user"),
query_factory=lambda: db.session.query(User),
widget=Select2ManyWidget(),
default=[],
),
The result will look like the image below

how to create new tag or select from current tags using Django forms

I have 3 tables, Products, Articles, and tags.
products and articles both have a "tags" field which has ManyToMany relation with the "tags" table
I want to be able to reference multiple tags or create new tags while adding a product or an article (in one field)(using Django forms)
for example, "test1" and "test2" are already in the tags table, I want the field to be like this:
1.I type "te" in the tags field
2.A drop down with "test1" and "test2" is opened which I can choose each one of them to be added in the field
3.I type "test3" (which isn't already in the tags table)and hit enter and "test3" is added to the field
products and articles both have a "tags" field which has ManyToMany relation with the "tags" table
(just like tags in a post in StackOverflow)
I think Django built-in forms can handle this but I'm overwhelmed by the documentation and lost in the configuration of my Django forms.
what kind of fields and widgets should I use for this purpose?
Excuse me if the question is vague I'm new to Django and I would appreciate any kind of help.
I figured it out. I did it with a combination of an Ajax call and its endpoint for searching in the current tags plus some JS to get the JSON data and put it in the field and finally in the Views file I implemented some logic for this field of the form so the data would be saved in the correct normalized format after the form submission.

How do I get the value of a WFFM field as a tag and output it in a Sitecore DMS report?

If I create a Web Forms For Marketers form with Analytics enabled I can choose to add each field as a tag to a Visitor. I can't see how to configure which tag they should be added to, or even what the tag is called by default (I'm assuming a tag with the field name is created).
I'd also like to know how to retrieve the tag data in a visit report (i.e. the one you'd get if you double clicked on a form submission in the Form Reports dialogue). I can see how to access plenty of inbuilt tags, but I can't find out how to fill these specifically from the form, and I cant see any fields in the report designer representing the field names I have.
Question 1: How to set the name of the tag
If you set the "Tag" checkbox on the form field, the Item Name (=field name) of the form field is used as tag name.
If you have database access, you can check the "VisitorTags" table on the analytics database to see which tags are written and how they are called.
Question 2: Retrieve Tag data in visit reports
In the VisitDetail report, the following inbuilt tags will be displayed if set:
Email
First Name
Second Name
Company
Organization
Full Name
StateProvince
Name your form fields accordingly and the values will be used in the report out of the box.
If you want to use custom tags in reports, have a look at the .mrt files in /sitecore/shell/Applications/Reports/. You will have to extend the report to use your own tags.
Example: Adding a custom tag to the VisitDetail report.
Extend the SQL Query to fetch tags in the /sitecore/system/Settings/Analytics/Reports SQL Queries/Visits Visitor Tags item. Add the line
, MAX(CASE WHEN [TagName] = 'SomeCustomTag' THEN [TagValue] ELSE NULL END) [SomeCustomTag]
Extend the VisitDetail.mrt, add a column with value SomeCustomTag to the VisitorTags section just like the predefined tags.
Use the value of your custom tag inside the report text by using {Visit.VisitorTagsRelation.SomeCustomTag}
I use a text editor to edit the .mrt files, but you can probably also do it in Reports Designer.

Using jquery autocomplete Django form

I am using jquery autocomplete with my generic views on Django. I am getting the list via AJAX, but there is one problem. When the user get the category he wants, he will select that value. Thus, if I append the value on that field $("#id_category").val(ui.item.value); it will show the pk on that field instead of value, so if I append $("#id_category").val(ui.item.label); django will complain it should be the instance. Django is completely right. How to make this work?
Edit:
I have made id_category hidden field and added category_display to append the text value. It is working. But while editing the category_display would be empty. Again I am using generic view. How to solve this one on the edit?
I had similar issue and I suggest you to think about question: 'What will happen if user enters not valid category name(category with this name doesn`t exist)?'
if you need to accept entered value and add new category, you have to make clean_category method in form:
def clean_category(self):
name = self.data['category']
return Category.objects.get_or_create(name=name)[0]
and display ui.item.label for user
if new category isn`t acceptable you can raise error in clean_category method or use something like select2(http://ivaynberg.github.io/select2/) for your field. I choose second variant.

'Hiding' form query from URL (Django 1.3)

I have a form with 6-7 fields. After user input, my webapp searches for those fields in a database and displays the results.
Now the issue is, that the URL ends up having all the form field names and their values in it.
result/?name=lorem&class=arc&course=ipsum
Now with the form having 7-8 fields the url ends up looking ugly.
Is there a Django technique to 'hide' these from the URL? Quotes around hide because I'd be okay with a completely different way to pass the objects to my database from the form as well.
Use a POST request. Here's the django docs on forms and a specific example using POST>. HTML-wise, all you need to do is change the method on the form tag.
I do not recommend to use POST requests for search. If you'll use GET it will be easer for user, he can just bookmark a link and save search or share search results with friends.