Cannot order by two fields using django filter in graphene-django - django-filter

I am using DjangoFilterConnectionField in my project like this:
all_sessions = DjangoFilterConnectionField(SessionNode, filterset_class=AgendaFilter)
SessionNode is created based on Session model in my Django application. Now, I would like to be able to order these sessions by two fields: start_date and start_time.
To achieve that I've created the following filter:
class AgendaFilter(FilterSet):
class Meta:
model = Session
exclude = []
order_by = OrderingFilter(
fields=(
("start_date", "start_date"),
("start_time", "start_time")
)
)
When I filter sessions by only one field using orderBy , the query results are ordered correctly as expected. When I try to use both fields in the filter (shown below), the results returned are not ordered according to either of them:
{
allSessions(orderBy: "[start_date, start_time]") {
edges {
node {
id
startDate
startTime
}
}
}
}
I've tried different ways of passing the two fields to orderBy, but none of them worked for me. How can I correctly order by start_date and then by start_time in one query? According to the graphene documentation, this is possible:
Ordering
You can use OrderFilter to define how you want your returned results to be ordered.
Extend the tuple of fields if you want to order by more than one field.
Is this is a bug in graphene or am I doing something wrong?

Related

Rails dynamically add condition to mysql query

How to add condition dynamically to sql query
for example if i have one element than it will look like
query=['one_element']
User.where('name LIKE ?, %"#{query[0]}"%')
but if it more than one
User.where('name LIKE ? and LIKE ? and Like... , %"#{query}"%', ..so on)
Im use myslq
so my main goal to split search query if it contains more than 2 words and search by them separately in one sql query
not where(name:'john dou') but where(name:'john' and name:'dou')
If you have a version of MySQL that supports RLIKE or REGEXP_LIKE:
User.where("RLIKE(name, :name_query)", name_query: query.join("|"))
Otherwise, you'll have to manually build with the ActiveRecord or operator:
# In the User model
scope :name_like, ->(name_part) {
return self.all if name_part.blank?
where("name LIKE :name_query", name_query: "%#{name_part}%")
}
scope :names_like, ->(names) {
relation = User.name_like(names.shift)
names.each { |name| relation = relation.or(name_like(name)) }
relation
}
Then you can pass it an array of any name partials you want:
query = ["john", "dou"]
User.names_like(query)
First split the word by it's separator like this query.split(' ') this will give you array of words. Then you can use like below in rails.
User.where(name: ['John', 'dou']

Groupby using GORM or MySql

My table has three column
created(Date)
score(int)(value is between 1-3)
id(long)
currently I am using Grails(GORM)
def listData= DaSes.createCriteria().list([max: 5, order: 'desc', sort: 'created']) {
projections {
groupProperty('created')
count('id')
}
}
[[date,count(of all scores)],[date,count(of all scores),...]
But I want result like this
[[date,count(of all scores),count(score1 for current day),count(score2 current day),count(score3 current day)],....]
Please guide me in this .
This is logically not possible.If you are using Group by on multiple columns then it gives
And this result is correct.

Get all rows who have no associated data

Campaign have attributes :start_date,:end_date
Invoice have attributes :start_date,:end_date
campaign.rb
has_many:invoices
invoice.rb
belongs_to:campaign'
I want to get all campaigns who have no invoices and whose end_date is less than current date.
I tried like this
Campaign.includes(:invoices).where("compaigns.end_date > ? ",Date.today, :invoices => { :campaign_id => nil } ).count
How I do this?
I think you're almost there. The example given doesn't work because it mixes the string and hash forms of where. Try splitting the conditions into two where calls:
Campaign.includes(:invoices).
where("campaigns.end_date > ? ",Date.today).
where(invoices: {campaign_id: nil}).count

Sharepoint 2010 Blog - Order rest query by Category

I've created a blog on Sharepoint 2010 and want to query the list via REST for reporting. I want to order the list by the default field Category (internal name PostCategory). Unfortunately, this is a multiselect field, therefore a simple "?$orderby=Category" doesn't work. I've also tried to expand the Category, but that doesn't work either.
Is there a chance, that I can order the list using rest? What about more then one selected Category? Can it be ordered by the first category, then the second, etc.?
If it's not possible using REST, what about ordering within JSON? I use a small javascript, that puts the list in a reporting format. Can I order within the JSON result?
Here is an example:
// Create REST-API URL
var strURL = "<REST-URL>";
// Get information from REST-API and create html output
$.getJSON(strURL, function(data) {
<Create output>
};
// Append to webpart
$('#<WebPartTitle>').append($(html));
EDIT: I've posted the question also here, since it's happening all in sharepoint
Category field (PostCategory internal name) is a multiple choice field, in SharePoint REST it is not supported to apply $orderby query option to this type of field.
But you could sort returned items using JavaScript.
The following example demonstrates how to order Posts by Category field.
There is one important note here:
Since Category field is a multiple choice field value, it is
assumed that only one category could be specified per post.
For that purpose FirstCategoryTitle property is introduced which
represent the title of first category in post item. This property is used > for sorting items
Example
var endpointUrl = 'http://contoso.intranet.com/blog/_vti_bin/listdata.svc/Posts?$expand=Category';
$.getJSON(endpointUrl, function(data) {
var items = data.d.results.map(function(item){
item.FirstCategoryTitle = (item.Category.results.length > 0 ? item.Category.results[0].Title : ''); //get first category
return item;
});
items.sort(postComparer); //sort by category
items.forEach(function(item){
console.log(item.Title);
});
});
function postComparer(x,y) {
return x.FirstCategoryTitle > y.FirstCategoryTitle;
}

MySQL: Merging different entities in one Query

I basically have three different classes of items that I want to show on a users wall: ratings, comments, and updates. This three are completey different entities, but because they all can appear on a users wall, I just call them "wallitem". The all have a timestamp property, which represents the date they were created.
I want to enable users to page through the wallitems, ordered by the timestamp. For example: last 10 wallitems. Or wallitems 20 to 30. Is there an MySQL Query that gives me the last 10 "wallitems", even though all different entities have different columns?
I could imagine, getting a list of items back, where each item has all the properties of all different entities, an additional property defining the type (for example "rating"), if it is in fact a rating, all other properties are just null. I would love to use such a dicationary in my php code:
foreach ($wallItemArray as $item) {
if ($item['type'] == "rating") {
$rating = $item; // use it as normal "rating"-entity
} else if ($item['type'] == "comment") {
$comment = $item; // use it as normal "comment"
}
// and so on for all different entities that could represent a wallitem in this context
}
Something like
SELECT 'rating' AS type, value AS r_val, NULL AS c_val
FROM Rating
UNION
SELECT 'comment' AS type, NULL AS r_val, comment_text AS c_val
FROM Comment
would get you started