Passing multiple values per key into template via ajax - django

I'm wanting to pass information relating to orders into my template via an ajax call. The problem is that that I've got more than two values that I want to pass in.
Key: Email address
Values that pertain to each key that I want to pass in:
Invoice Date
Name
Phone number
I'm wondering what the best way to do this is. Does the dictionary value need to be a list?
eg.
order_dict[order.email] = ['order.invoice_date', 'order.name', 'order.phone']
If the dictionary value does need to be a list, how would I iterate over it in my template?
Thanks for your help!

Related

Django Dynamically Calculate Average

I've multiple fields in my model, and I need to remove the average of only the columns user inputs
Could be
How can I do it dynamically?
I know I can do
mean = results.aggregate(Avg("student_score"))
This is one, I want to add multiple Avg statements dynamically
I tried making a loop as well to get all names and add all fields given by user one by one
eg - Avg('students'), Avg('playtime'), Avg('grade'), Avg('sales')
But I get
QuerySet.aggregate() received non-expression(s): <class 'django.db.models.aggregates.Avg'>('students'), <class 'django.db.models.aggregates.Avg'>('sales').
I've even tried raw query, but it needs a unique ID because of which that isn't working
Any workaround ideas?
I am using MySQL DB
Aggregate return single result from the list of objects. You need to annotate if you need multiple result like following,
YourModel.objects.values("YOUR GROUP BY VALUES HERE").annotate(Avg('students'), Avg('playtime'), Avg('grade'), Avg('sales')

How can I access list values in a WFFM list field?

I'm trying to make a poll results page with the results from a WFFM form. I initially took all the entered data(using AnalyticsFormsDataProvider), queried it, and then displayed the results. However, if no one votes for a particular answer, it wouldn't be in the entered data so it wouldn't show.
I want to display that the field had 0 votes, so now I'm trying to compare the possible answers to the queried list. However, I can't seem to find a way to access the values of the list items. Does anyone know the class those values are stored in and/or how to access them?
I'm using WFFM 8.1 rev. 151217 (Update-1) with MVC. It's a radio list field if that makes a difference from the other list fields.
Thanks,
You are able to get WFFM field as regular Sitecore item via standard API.
Available list items values are saved in "Parameters" and "Localized Parameters" fields. You can use Sitecore.Form.Core.Utility.ParametersUtil from Sitecore.Forms.Core to parse values saved in these fields.
This data is stored on your Radio List Field's Item in the fields Parameters and Localized Parameters.
If your code is within the Form's SaveAction or a Validator you will have access to the Field in the for AdaptedResultList args the SaveAction or via this.classAttributes["fieldid"] for the Validator. That will be enough for you to get the Radio List Item and then the Parameter fields.
If your code is not in a SaveAction / Validator I recommend passing the Form Item into your code possibly by Datasource and then retrieving the Radio List Item from that.
Finally the Items of the your Radio List field is stored in the format
<item><item>
If your radio items are actually items you will want to parse them using a regex
var regexItems = new Regex(#"<item>(.*?)</item>".ToLower());
var itemNodes = regexItems.Matches(FieldItem["Parameters"].ToLower());
Then you should be free to play with the list for your poll

Django Session - set two related data into one session variable

I have these following two data which I want to save into session, so that I can call all of them in last-preview page:
1) product title, 2) product size
I am wondering how to save this into session. My problems are: Session keys are unique so I cannot name the session name with some name. another issue is that these 2 data are coming dynamically. So I cannot use the product title as session key.
I want that both data is saved in one session variable.
Session variables are simply stored as the json-serialization of the actual value. This means that you can use lists, tuples and dictionaries, and any other json-serializable values.
One solution is to store your products as a list of (name, size) tuples. Then you can do:
if request.session['products']:
request.session['products'].append(('name', size))
else:
request.session['products'] = [('name', size)]

Django annotate according to foreign field's attribute

I normally use something like this "Tag.object.annotate(num_post=Count('post')).filter(num_post__gt=2)" to get tags with more than 2 posts. I want to get number of posts with a field value (e.g post.published=True) and annote over them so that I get tags with number of published posts bigger than some value. How would I do that?
Edit:
What I want is not filter over annotated objects. What I want is something like this: Tag.objects.annotate(num_post=Count("posts that have published field set to true!")). What I am trying to learn is, how to put post that have published field set to true in Count function.
You can just replace the 2 in ..._gt=2 with some other variable - for example, a variable that gets passed into the view, or a request.GET value, or similar.
Is that what you're trying to do?

Variable number of fields in Django URL

I'd like a URL of the form:
... field1/eq/value1/field2/gt/value2/ ...
Where I want to filter the contents of the page in the view function based on an arbitrary number of fields (the names of which are not known in advance).
I've tried:
(r'^((?P<field>\w+)/(?P<op>[a-z]+)/(?P<value>\w+)/)*$', my_view)
But the keyword arguments are filled with the last set of three field/op/value to occur in the URL.
Is there any way that I could populate a list or dictionary based on a variable number of URl fields like this?
Or should I be doing this some completely different way?
Don't do this.
Use the query string. ?field,eq,value&field,gt,value will work out much better.