I found it kinda troublesome or difficult to find exact example I wanted for GQL.
Given the following:
class Sample (db.Model):
name = db.StringProperty(required=True)
type = db.StringProperty(required=False)
How do I set default parameter value.Let's say, I want to set type parameter to the value of "new" when I do following:
Sample(name="Yeah").put()
What would be the format of setting the default parameter value for a class inherited db.Model ?
Just add "default" parameter in the definition of your type, like this:
class Test(db.Model):
Test = db.StringProperty(default="test")
Related
I accidentally named a Core Data class Set. I can't change the class name because the model is already on CloudKit.
But now I can't use the Set struct. Is there anything I can do to use Set structs when I want to? Can I use NSSet instead? Will that impact anything considering I'm using Swift/SwiftUI?
You can specify type of object like this:
class Set {}
let a = Swift.Set("1") //swift's Set instance
let b = Set() //your custom Set instance
let c = YourApp.Set() //your custom Set instance
I know that generally, we need to do something similar to this for getting a document back from mongodb in spring data:
Define a class and annotate it with #Document:
#Document ("persons")
public class Person
Use MongoTemplete:
mongoOps.findById(p.getId(), Person.class);
The problem is that in runtime I don't know the class type of the document, I just have its string collection name and its string Id. How is it possible to retrieve the document using SpringData? Something like this:
db.myCollectionName.findOne({_id: myId})
The result object type is not a concern, it can be even an object, I just want to map it to a jackson JsonNode.
A possible workaround for this you can use the aggregate function of mongooperation like this
AggregationResults<Object> aggResults = mongoOps.aggregate(newAggregation(match(Criteria.where("_id").is(myId)) ,
myCollectionName, Object.class);
return aggResults.getUniqueMappedResult();
Im following the Angular2 cookbook recipe for Dynamic Forms. It calls for
export class QuestionBase<T>{
value: T,
...
I cant seem to find out what the is doing in both spots. Any ideas?
Those are so called "Generics". You may just google for the term "generics", for example in combination with "typescript", in order to get a more detailed answer.
The quick version is:
with generics you do not care which type T is - as long as it is the same type everywhere you use it.
So an instance of
QuestionBase<String>
has to make sure that the property "value" is of type String
The T on the QuestionBase<T> stands for Type.
This means that whatever type you enter in the <> that is the type that value will have.
So,
If T = string:
export class QuestionBase<string>{
value: string,
If T = int:
export class QuestionBase<int>{
value: int,
If T = any:
export class QuestionBase<any>{
value: any,
If T = any[]:
export class QuestionBase<any[]>{
value: any[],
T here makes your class a template..which means your class can be of any type
while creating the object of QuestionBase you can say like below
var obj=new QuestionBase<string>();
now here your value property will be of string type.
Similarly
var obj=new QuestionBase<int>(); value property will of type int here.
I've following model set up initially
class Obj (db.Model):
name = db.StringProperty(required=True)
rating = db.IntegerProperty(default=0, required=False)
There are entities already created with above, such as:
name="test1", rating="3"
So, I need to change the rating type to float. I was trying to achieve this with db.Expando
class Obj (db.Expando):
name = db.StringProperty(required=True)
rating = db.FloatProperty(default=0, required=False)
Before I'm able to retrieve the instance of the Obj model to update it to float value, I've already got the following error:
Property rating must be a float
At first, I got this error, because I wasn't using db.Expando. However, after using db.Expando, I assumed this error shouldn't come into place ? Since it can dynamically change value, type etc as I read the articles.
Being new to db.Expando, I need help. Does anyone have clue to what happened ?
EDIT
for o in Obj.all():
a = []
if o.rating:
o.rating = float(str(restaurant.rating))
else:
o.rating = float(0)
a.append(restaurant)
db.put(a)
After having above code, the same error pops up
Property rating must be a float
SOLUTION
Temporarily removed the rating from model definition and updated the values to float first and then add the new rating definition with db.FloatProperty
A db.Expando model allows you to add properties that aren't defined in the class itself; however, any properties that are explicitly defined in the class do need to be the correct type.
Simply removing rating from the model definition may work for you.
I'm trying to turn an string into an instance name.
stage.focus = ["box_"+[i+1]];
this gives me back = box_2;
but I need it to be an object not a string.
In as2 I could use eval. How do I do it in as3?
The correct syntax is:
this["box_"+(i+1)]
For example if you would like to call the function "start" in your main class, you'd do it this way:
this["start"]();
Same thing goes for variables. Since all classes are a subclass of Object you can retrieve their variables like you would with an ordinary object. A class like this:
package{
import flash.display.Sprite;
public class Main extends Sprite{
public var button:Sprite;
public function Main(){
trace(this["button"]);
}
}
}
Would output:
[object Sprite]
If you want to access a member of the current class, the answers already given will work. But if the instance you are looking isn't part of the class, you are out of luck.
For example:
private function foo():void {
var box_2:Sprite;
trace(this["box_"+(i+1)]);
}
Won't work, because box_2 isn't a part of the class. In that case, it is highly recommended to use an array.
If you want to access a DisplayObject (for example, a Sprite or a MovieClip) you also can use getChildByName. But in that case, box_2 will be the name of the object, instead of the name of the variable. You set the name like
var box:Sprite;
box.name = "box_2";
But again, I recommend an array.