django "use_natural_foreign_keys=True" issue - sql

I currently use the well documented "use_natural_foreign_keys=True" to return the relevant field data required instead of the id:
all_orders = Orders.objects.all()
resp = serializers.serialize('json', all_orders, use_natural_foreign_keys=True)
What I don't know how to do is return both the id AND the field data required as typically returned by the "use of use_natural_foreign_keys=True".
Anyone know of a quick fix to return both?
Many thanks, Alan.

define a "natural_key" method in your model class, whose id and field_name you like to get. e.g
def natural_key(self):
return (self.id, self.field_name)

Related

solrj getCountDistinct() return null?

I am trying to get unique value of a field with the code:
query.set("q","*:*" );
query.setGetFieldStatistics(true);
query.setGetFieldStatistics("popu_s");
QueryResponse rsp = solr.query(query);
FieldStatsInfo stats = rsp.getFieldStatsInfo().get("popu_s");
System.out.println(stats.getCount());
System.out.println(stats.getCountDistinct());
stats.getCount() gives the correct count. However, stats.getCountDistinct() always returns null.
Any idea?
getCountDistinct() in FieldStatsInfo is null because it was not provided when creating the FieldStatsInfo object (what was returned from solr.query(query)).
There is no guarantee that any of the fields in this pojo are populated.
As an alternative try using stats.getCardinality() although again there is no guarantee this is poulated.
Found the solution!
To make getCountDistinct() return a value, one needs to add this
query.addStatsFieldCalcDistinct("popu_s", true);

ActiveRecord outputs too much in select query for Rails

According to:
#actors = actors.to_sql
this query:
actors = Actor.select("actors.name")
supposed to be equal to:
SELECT actors.name FROM `actors`
And it works in my SQL; only selects actors.name ... so i try:
#actors = actors.to_json.to_s
and render on some view... I get this;
[{"name":"James Patrick Pe","id":null,"logo":{"url":"uploads/default.jpg","topbar_avatar":{"url":"uploads/topbar_avatar_default.jpg"}}},{"name":"SSS","id":null,"logo":{"url":"uploads/default.jpg","topbar_avatar":{"url":"uploads/topbar_avatar_default.jpg"}}},{"name":"Philhealth","id":nul ....
it's basically a JSON of everything... I only need one column for an AJAX reply. How can I do this with the ActiveRecord Way?
Do you use Paperclip? It's likely you have a method that appends something to your JSON.
What you need to do is to specify the fields you want to be exported.
#actors = actors.to_json(only: [:name]).to_s
Also please note that there is a better approach to to_json. You should definitely have serializers that take an object or collection in input and deal with the serialization.

PDO fetchColumn() and fetchObject() which is better and proper usage

It's been bugging me, I have a query which returns a single row and I need to get their corresponding column value.
//Retrieve Ticket Information to Database
$r = db_query("SELECT title, description, terms_cond, image, social_status, sched_stat FROM giveaway_table WHERE ticket_id = :ticket_id",
array(
':ticket_id' => $ticket_id
));
There are two ways that I can get data which is, by using fetchColumn() and fetchObject()
fetchObject()
$object = $r->fetchObject();
$ticket_info[] = $object->title;
$ticket_info[] = $object->description;
$ticket_info[] = $object->terms_cond;
$ticket_info[] = $object->image;
$ticket_info[] = $object->social_status;
$ticket_info[] = $object->sched_stat;
fetchColumn()
$title = $r->fetchColumn() //Returns title column value
$description = $r->fetchColumn(1) //Returns description column value
Was wondering, which one is better, or are there any pros and cons about this stuff?
if possible, can you guys also suggest the best way (if there's any) on how to retrieve all columns that's been selected in a query and store it into an array with less line of code.
There are two ways that I can get data which is, by using fetchColumn() and fetchObject()
really? what about fetch()?
There is a PDO tag wiki where you can find everything you need
I don't know pros and cons of using it. In my project I often used fetching as array rather than object. It was more comfortable. But if you make ORM projects then maybe it would be better to use fetchObject and make it your object not a std_class. You could make a contructor that has one parametr which is stdClass and make your object from this class
Answering your other question you can fetch all columns using fetchAll();
Follow this link to learn more about this function http://www.php.net/manual/en/pdostatement.fetchall.php
More about abstract database layer you can find here -> http://www.doctrine-project.org/

Rails getting single values from request

in Rails the create method in a Controller by default receives an HTTP request with different values.
By default new records are created like this:
#apo = Apo.new(params[:apo])
But how can i access single Values from this params hash?
I would like to create something like this:
#apo = Apo.new do |a|
a.name = $someVariable
a.value = $anotherVariable
a.quantity = -> here i want to have one value which is in params[:apo]
end
Do you understand what i´m looking for?
Tried million possibilities but it just doesn´t work.
Alternatively, is it possible, to create a second params hash in the view, which only saves this one value?
P.S. i don´t want to use JavaScript for doing this...
Thanks a lot!
params is special, and is set by Rails for each HTTP request. It's a hash in the form
{ :object => { :attrib1 => "value1", :attrib2 => "value2" ... }}
So you can reference the entire object with
params[:foo]
and individual attributes (fields) like
params[:foo][:bar]
A ActiveRecord model can be created in one call by passing a hash of values, as in your first example. But there are many other ways to create an instance. You can
def make_apo(some_value, another_value)
apo = Apo.new
apo.name = some_value
apo.value = another_value
end
Such a method will return an instance of Apo. In your case, if you have some values in params, change the above to accept params as another argument, or pass specific values when you call.
def make_apo(some_value, another_value, quantity, passed_params)
apo = Apo.new
apo.name = some_value
apo.value = another_value
apo.quantity = passed_params[:apo][:quantity]
end
But this is all a pretty unusual way of going about things. So don't just do this -- it's more by way of explaining what's going on than suggesting that you do this.

List Members based on extended profile fields in buddypress

I'm working on getting a list of members based on the fields they selected on the extended profiles fields in buddypress. Here is my code:
<?php
$membership_group = "Orange Membership";
$db_query = "SELECT user_id FROM wp_bp_xprofile_data WHERE field_id = 33 AND value = \"" .$membership_group ."\"";
$match_ids = $wpdb->get_var($db_query);
$get_these_members = 'include=' .$match_ids;
if (bp_has_members($get_these_members, 'per_page optional=9')) {
//Some Codes here
}
?>
The result is returning just the first member it gets from the query instead of a list of members. Please say what I'm doing wrong.
Thanks
I think you should dive into the class BP_Core_User and its method get_users. It supports meta_key and meta_value.
You can also try to make just a search by field value. So pass an argument s to bp_has_members.
And per_page optional=9 is a wrong syntax.
This:
$wpdb->get_var($db_query);
returns a single var !
This is what you want:
$wpdb->get_col($db_query);
Then fix the syntax error mentioned by slaFFik