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

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.

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

Save the dynamically populated value on dropdown

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.

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.

joomla 2.5.6: how to fetch all articles by category id

I am using Joomla 2.5.6 and using my custom query wants to fetch all articles by category id. I study Joomla 2.5.6 db model and found xma7k_content table is stores Articles meta and xma7k_categories stores Category info & it seems that we can easily fetch records by id, the alias in xma7k_content table is exactly what the article tile is but the path to view the article is some thing like
destinations/2012-08-08-05-05-05/thailand/overview-of-trips
which stored in xma7k_menu table.
If I remove date/time from path while creating article. (But how I
could?)
or how I could use JOIN xma7k_content, xma7k_categories &
xma7k_menu tables to get required result
Use Acesef Joomla extension and you can define your own SEF URL for the page, download here http://www.joomace.net/joomla-extensions/acesef-joomla-seo-sef-urls.
Check the document for more info.

How to display related tags in django tagging?

i am using django tagging. can anyone give any example as to how can i show the related tags, when the object related to a particular tag is displayed? Something like similar tags in stackoverflow.
Thank you!
You can use the get_related manager which will:
Retrieve a list of instances of the specified model which share
tags with the model instance obj ordered by the number of
shared tags in descending order.
To use this you could create a template tag such as:
#register.inclusion_tag(your_template)
def related_objects(object, limit=3):
objects = TaggedItem.objects.get_related(object,object.__class__)
return {'objects': objects[:limit]}
Edit for comment
to get a list of similar tags you can use related_for_model, which will return "other tags used by items which have all the given tags"