How to display related tags in django tagging? - django

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"

Related

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.

Form of a list of instances

I am missing an idea or a way to handle this in django.
I have a list of instances (class Sample) and I want to present the last 10 of them in a form with a checkbox before... I want to have a button "work on these" to submit the checked instances to a view. This view should present a detailView of the selected instances.
I don't know, how to handle this exactly. Can somebody give me a hint? All I did so far was just creating a form of a instance.
Thanks in advance.
You can use a ModelChoiceField or a ModelMultipleChoiceField.
class MyForm(forms.Form):
my_list_of_instances = forms.ModelMultipleChoiceField(queryset = MyModel.objects.all().order_by('-id')[:10], widget=forms.CheckboxSelectMultiple)
This should generate a list of checkbox with your last 10 instances created.
With this you have your form ready to receive your input...just point your action to your view.
Edit:
In case you want to have several forms, one for each instance, you can work with Formsets. You would not get the checkboxes tho...you would get any change applied to your instances. If you want to have both (checkboxes and formsets) you can use both suggestions, using the checkbox values sent to the server to filter the formsets you are going to save.

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.

django-taggit create tagcloud from queryset

I could not find an answer for this.. So here my question. For a new project i'd like to use django-taggit.
Does someone have a suggestion on how to create a tag-cloud based on the current queryset?
The desired behavior is to 'start' with an unfiltered list - then allow narrowing town the results with applying filters and tags. At the beginning the tag-cloud shows e.g. the 50 most common tags. After choosing a tag (or other criteria) the tag-cloud should only display the remaining possibilities.
I know that django-tagging offers Tag.objects.usage_for_queryset() for this situation. But I would prefer to use '-taggit' over '-tagging'.
django-taggit-templatetags appears to be the 'go-to' place for a tagcloud for django-taggit.
It doesn't appear to handle querysets though. :(
So, I've added:
#register.inclusion_tag('taggit_templatetags/tagcloud_include_qs.html')
def include_tagcloud_qs(queryset):
try:
queryset = queryset.annotate(num_times=Count('taggeditem_items'))
except FieldError:
queryset = queryset.annotate(num_times=Count('taggit_taggeditem_items'))
num_times = queryset.values_list('num_times', flat=True)
weight_fun = get_weight_fun(T_MIN, T_MAX, min(num_times), max(num_times))
queryset = queryset.order_by('name')
for tag in queryset:
tag.weight = weight_fun(tag.num_times)
return {"tags": queryset}
to
templatetags/taggit_extras.py
And this to a new file at taggit_templatetags/tagcloud_include_qs.html
<div>
{% for tag in tags %}
<font size={{tag.weight|floatformat:0}}>{{tag}}</font>
{% endfor %}
</div>
I'm using it like this in my templates:
{% include_tagcloud_qs my_queryset %}
I haven't spent much time looking at the django-taggit-templatetags code, so feel free to update this with a better solution!
PS:
I'm getting a queryset in my view like this:
my_queryset = Tag.objects.filter(foo__bar=baz).distinct()
This answer shows how to build a tag cloud. You'd create a queryset in your view according to your parameters, generate a dictionary, and render it in your templates as shown in that answer.
I would suggest using django-tagging. It is well documented. I have created tag clouds with it. You can access tag clouds via model, model instance, etc via template tags that are easy to load. This is a little hackish but using the .counts method you can hack up some css to increase the size of each font as you would see in a real tag cloud. Django-tagging actually excels in this area as it has a default template tag with formatting options for everything you have described.
I've added a TagBase.get_for() function in https://github.com/twig/django-taggit/commit/42cd4e04f00496103f295c0afd8297074be50dcf
This basically fetches the Tags used for a given queryset, and from there you can do what you need to do.

Django multiple many to many fields?

I am building a news app for my website. I want to use a sort of tag system. Each news article can have different and multiple tags. All tags are saved in a tag model, and i want to connect the tags to the newsarticle. Now is this possible with: tags = models.ForeignKey( TagsModel ) for one tag, but how i can do this with multiple of them?
Thank you!
Use django tagging.
In your model you do: tags = TagField() and presto, you have tags that behave like you expect. The app also comes with several niceties to perform common tasks. e.g. parse input into tags or output the tags in templates.
In general, though, you can have ManyToMany fields like so:
some_things = models.ManyToManyField(OtherModel)
here are the docs for that. You can have multiple ManyToManyFields in a single model, you just need to specify related names.