i have very simple newbie questions params hashs here, this is the following code of params hash from my view,
{ name: 'name supplier',
fax: 'fax supplier',
contact_persons: [
{name: 'contact person 1', value_number: '123456', value_type: 'mobile phone'},
{name: 'contact person 2', value_number: '123456', value_type: 'mobile phone'},
{name: 'contact person 3', value_number: '123456', value_type: 'email'},
{name: 'contact person 4', value_number: '123456', value_type: 'bb'}
]
}
i want to save it on db, but how i do that, its not just simple ContactPerson.new(params[:contat_persons]) right??
Assuming you have a has_many relationship between your Supplier and ContactPerson you could do something from supplier.rb like:
params[:contact_persons].each { |person| contact_persons.build(person) }
contact_persons.save
Related
I have the following string received from an ajax post in a django view (its 2 rows of a angular ui-grid):
data=[{"part_name":"33029191913","id":"5","qty":"3","ticket":"","comments":"test"},
{"part_name":"3302919777","id":"3","qty":"323","ticket":"","comments":"test"}]
Then doing json.loads(data) that gives me the following:
data={'part_name': '33029191913', 'id': '5', 'qty': '3', 'ticket': '', 'comments': 'test'}
{'part_name': '3302919777', 'id': '3', 'qty': '323', 'ticket': '', 'comments': 'test'}
Without specifying each field in order to create a dictionary, I want to create one that looks like the following in order to insert it into my model, via MyModel(**data):
{'part_name': '33029191913', 'id': '5', 'qty': '3', 'ticket': '', 'comments': 'test'}
After json.loads(data), if i try to access one element (eg. data[0]['part_name']), it correctly shows '33029191913', but if I do data[0] or data[1], it gives me only the apparent keys:
part_nameidqtyticketcomments.
How can I separate the string received by ajax into dictionaries in order to insert them into the django model?
Taking this json string from a post:
data=[{"part_name": "part 1", "part_id": "1", "quantity": "233", "ticket": "32", "comments": "32"}{"part_name": "part 1", "part_id": "1", "quantity": "233", "ticket": "32", "comments": "32"}]
grid=ast.literal_eval(data) returns:
{'part_name': 'part 1', 'part_id': '1', 'quantity': '233', 'ticket': '32', 'comments': '32'}{'part_name': 'part 1', 'part_id': '1', 'quantity': '233', 'ticket': '32', 'comments': '32'}
Assuming that the keys on the ajax json string are the same as your model fields(only the 'part_name' field is not in the model, and hardcoding the order_id foreign key for test purposes):
for d in grid:
object_insert={}
for key in d:
object_insert[key]=d[key]
object_insert['order_id']=1
del object_insert['part_name']
order=Order_Detail(**object_insert)
order.save()
This saves the dictionary in the model without having to specify each of the keys.
How to index nested documents using Boto into dynamo? Or is it even possible?
I tried the following but it just hangs...
CASE A
users.put_item(data={
'username': 'johnd',
'first_name': 'John',
'last_name': 'Doe',
'account': {'type': 'standard', 'age', '5'},
})
Along the same vein of nesting:
CASE B
users.put_item(data={
'username': 'johnd',
'first_name': 'John',
'last_name': 'Doe',
'account': {'type': {'level':1, 'class':'standard''}},
})
Can it support arrays?
CASE C
users.put_item(data={
'username': 'johnd',
'first_name': 'John',
'last_name': 'Doe',
'accounts': [{'account_name': 1}, {'account_name': 2}]
})
Can it support nested arrays?
CASE D
users.put_item(data={
'username': 'johnd',
'first_name': 'John',
'last_name': 'Doe',
'accounts': [{'account_name': 1, 'types':['Checking']}, {'account_name': 2, 'types':['Savings Standard', 'Savings Plus']}]
})
If not, are there alternatives in or outside of python? Or is this not possible?
You can use the layer1 interface in boto to put nested structures. I'm not sure if it's available in the layer2 interface yet. There's an example in this thread on the AWS forums.
I'm a newbie to ember and I'm trying to create a basic sign-up form.
Relevant model:
App.NewUser = DS.Model.extend({
user_name: DS.attr('string'),
password: DS.attr('string'),
confirm_password: DS.attr('string'),
email: DS.attr('string'),
first_name: DS.attr('string'),
last_name: DS.attr('string'),
});
Relevant controller:
App.SignupController = Ember.ArrayController.extend({
actions: {
signup: function() {
var data = this.getProperties('first_name', 'last_name', 'email', 'user_name', 'password', 'confirm_password');
var newUser = this.store.createRecord('newUser', data);
newUser.save();
},
},
});
When the "signup" action executes, I get the following error:
Error: Attempted to handle event `didSetProperty` on <App.NewUser:ember332:null> while in state root.deleted.saved. Called with {name: last_name, oldValue: undefined, originalValue: undefined, value: undefined}.
What am I doing wrong?
This is a bug, Ember Data is setting the record state incorrectly if you're setting a value to what it's currently set to (undefined on createRecord)
You'll want to either coerce your values into empty strings or not set undefined values while creating the record.
for(var key in data){
if(!data[key]) delete data[key];
}
http://emberjs.jsbin.com/OxIDiVU/124/edit
https://github.com/emberjs/data/issues/1648
My server returns a JSON response like this:
{
artists: [{
id: "1",
first_name: "Foo",
last_name: "Bar"
}],
studios: [{
id: 1,
name: "Test",
// ...
artist_ids: ["1"]
}]
}
'artist' is in fact a User model but with a different name. How can I map artist to the User model? Maybe a bad explanation but if I rename the JSON response serverside to 'users' instead of 'artist' and use the models below everything works like I want. I simply want to use the name 'artist' instead of 'user', both server side and client side. Hope you guys understand what i mean.
App.Studio = DS.Model.extend
name: DS.attr 'string'
// ..
users: DS.hasMany 'App.User'
App.User = DS.Model.extend
firstName: DS.attr 'string'
lastName: DS.attr 'string'
studio: DS.belongsTo 'App.Studio'
I guess that the simplest thing to do would be something like artists: DS.hasMany 'App.User' but obviously this does not work.
First, I recommend using the latest Ember / EmberData, so relationships are defined like this:
App.Studio = DS.Model.extend({
name: DS.attr('string'),
// ..
users: DS.hasMany('user')
});
App.User = DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),
studio: DS.belongsTo('studio')
});
Next, I recommend using the ActiveModelAdapter if you are getting underscores in your response JSON:
App.ApplicationAdapter = DS.ActiveModelAdapter;
Finally, overriding typeForRoot and keyForRelationship in a custom serializer should fix your issue:
App.ApplicationSerializer = DS.ActiveModelSerializer.extend({
typeForRoot: function(root) {
if (root == 'artist' || root == 'artists') { root = 'user'; }
return this._super(root);
},
keyForRelationship: function(key, kind) {
if (key == 'users') { key = 'artists'; }
return this._super(key, kind);
}
});
Example JSBin
One last thing: you can even get rid of the custom keyForRelationship if you name the relationship artists in Studio:
App.Studio = DS.Model.extend({
name: DS.attr('string'),
// ..
artists: DS.hasMany('user')
});
Have you tried just creating an Artist model extended from User?
App.Artist = App.User.extend({})
I haven't tried it, but I suspect that might work.
I want to display an extjs combo with a JsonStore
Server side I use python/Django.
So, this is my combo:
xtype: 'combo',
store: new Ext.data.JsonStore({
url: 'get_peoples',
root: 'data',
totalProperty: 'total',
fields: [
{name: 'name', mapping: 'fields.name'},
{name: 'subname', mapping: 'fields.subname'}
],
autoLoad: true
}),
trigerAction: 'all'
and the views.py server side:
def get_peoples(request):
queryset = People.objects.all()
data = '{"total": %s, "%s": %s}' % \
(queryset.count(), 'data', serializers.serialize('json', queryset))
return HttpResponse(data, mimetype='application/json')
the get_people call give me
{"total": 1, "data": [{"pk": 1, "model": "myapp.people", "fields": {"name": "Paul", "subname": "Di Anno"}}
I think i'm not doing it right, cause my combo doesn't display any item
You need to add displayField and valueField declarations to your ComboBox so that it knows which fields from your store to use for either role. Also, setting autoLoad in the store is not necessary.
new Ext.form.ComboBox({
xtype: 'combo',
store: new Ext.data.JsonStore({
url: 'get_peoples',
root: 'data',
totalProperty: 'total',
fields: [
{name: 'name', mapping: 'fields.name'},
{name: 'subname', mapping: 'fields.subname'}
]
}),
triggerAction: 'all',
// Just guessing at the proper fields here.
// Set them to whatever you wish.
displayField: 'name',
valueField: 'subname'
});