In flask_marshmallow Can you declare a schema to return an array of objects - flask-marshmallow

So I have this schema that I want to return a list of objects, not as a field with a list of objects which I successfully can and do return. Just the array.
Is it possible?

Related

How to change gorm.Record type to array

currently I am using gorm to retrieve data from db (postgresql db to be specific) and scan it in an array, the data stored in db is also in form of array. So problem I am facing is after scanning data in empty int array it changes into gorm.Record type which can't be used for basic array operation like, appending, iterating, etc.
Here's a related part of my code:
var winner_selection []int64 // empty int64 array
db.Table("giveaways").Select("Participants").Where("Name = ?", name).Scan(&winner_selection) // scanning the value in array
How can I retrieve the data directly in form of array so the array remains array or is there anyway to change gorm.Record type to array?

Laravel - How to extract one field for items in a model to an array?

I have a table that has a one-to-many relationship with another table.
Call them tables parent and child
child has a field called field1
I am trying to get an array of all field1 values.
In the parent Model, I have this function that gets me all children of parent
public function children()
{
return $this->hasMany(Child::class);
}
Now, in the parent model, I also want to get all field1 values.
I can get all the children like so:
$children = $this->children->all();
But I just can't figure out how to then index into this to get all the field1 values.
Maybe it is better to just use $this->children in which case it is a Collection and then use some sort of Collection method to extract field1?
I tried
$this->children->only(['field1'])
but that returned nothing, even though field1 certain exists.
Ideas?
Thanks!
You can use pluck method of collecion to get an array of field1 values:
$parent->child->pluck('field1');
Or better, you can use pluck method of query builder (see section Retrieving A List Of Column Values), being even more efficient, with same result:
$parent->child()->pluck('field1');
map() is the function you are looking.
$children = $this->children->all()->map(function($children) {
return $children->field1;
});
A shorter version using Higher Order Message.
$children = $this->children->all()->map->field1;

How to Get a Nested Array Field

I have a table user which contains an array Employee which stores the names ,i need to retrieve the last element of the array.
Could not figure out.Tried with count but count gives value =1 but there are three elements in an array
r.table("user")("Employee").count()
I need count actual elements in Employee array which is 3.Please help.Thanks
If you don't call .count on a specific document's field, it will return the count of the sequence of documents. In your request, 1 as a result actually means there is one document in the table.
You need to loop over each document to fetch the last name for each document, and you can fetch it directly using .nth:
r.table("user").map(function(document) {
return document.merge({lastName: document("Employee").nth(-1)});
// or, if you like complicated stuff
return {
id: document('id'),
lastName: document("Employee").nth(
document("Employee").count().sub(1)
)
};
});

after filtering a plucked laravel collection, the indexed array change to Associative array

I have a collection of model eloquent such as user model, i use the pluck method to get the only post_idfrom this collection, this method give me the indexed array of post_id, but when i use filter or unique method for this indexed array the result change to Associative array. i don't want a assoc array in result. I want just the unique of post_id's in the indexed array. laravel auto changing my result.
$this->posts->pluck('post_id')->unique('post_id')
result is :{ "1": 1 , "2": 2 }.
Is this can a bug or I have a mistake in fetching data by methods?
You can use groupBy like this :
$this->posts->groupBy('post_id')->pluck('post_id');

How to update nested redux state

I have the following structure
lookups --> Object
lookups.CATEGORIES --> array of category objects
lookups.TIMEZONES --> array of timezone objects
I would like to add new object, which is a lookup object which has lookup_type property. It could be either 'CATEGORY' or 'TIMEZONE'.
Depending on lookup_type, the newly added object has to be added either to CATEGORIES or TIMEZONES object. How this could be achieved?
The structure of lookups object
lookups: {CATEGORIES:[{obj1}, {obj2}...], TIMEZONES:[{obj1}, {obj2}, {obj3}...]}
You can use spread on the nested object or array too:
return {
...state,
[action.lookupType]: [
...state[action.lookupType],
action.value,
]
};
That would add a new item to Categories or Timezone, if you want to replace a value or insert it at an index etc then you should construct the new array how you want it just above the return and pass that instead. Also note that array spread is ES7.
You probably want to pass your lookup object as the payload of an action, which your lookup reducer handles. In the lookup reducer check for the value action.payload.lookup_type and return the state with a new CATEGORIES or TIMEZONES array containing the old values of that array with the lookup object insterted. You should probably check out some redux examples first, if you are unsure how to work with it.

Resources