Django database introspection - database

I'd like to introspect the Django database(or table) at the runtime. So, for example- I'd like to do something like:
>>> a = django.db.introspect()
and now *a* should see like
a = {
'table_name1':{
'column_name_1_1':{
'index': True,
'unique': True,
'pk': True
},
'column_name_1_2':{
'index': True,
'unique': False,
'pk': False
}
},
'table_name2':{
'column_name_2_1':{
'index': True,
'unique': True,
'pk': True
},
'column_name_2_2':{
'index': True,
'unique': False,
'pk': False
}
}
}
And- I'd like to do that with Django & South and without any 3rd party tools(I know that I coul do that with SQLAlchemy). I want to introspect the actual db, not the frozen one in my last migration. Is that possible? How can I start?

I've found my answer- here is everything I need:
https://code.djangoproject.com/browser/django/trunk/django/core/management/commands/inspectdb.py

Related

express get all values as strings from angular POST request

I have a following simple code that send a POST request to a server with express:
$http.post('/blah', {
boolean: true,
stringBoolean: 'true',
number: 213,
stringNubmer: '44444444',
string: 'adssd',
arrayNumber: [1, 2, 3, 4],
arrayBoolean: [true, false, "true", "false"],
});
and this lines on server side:
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
app.post('/blah', (req, res)=>{
console.log(req.body)
})
the output in console after client send request(tested on chrome and firefox) will be with all values as string:
{ boolean: 'true',
stringBoolean: 'true',
number: '213',
stringNubmer: '44444444',
string: 'adssd',
arrayNumber: [ '1', '2', '3', '4' ],
arrayBoolean: [ 'true', 'false', 'true', 'false' ] }
Then I use Postman to send same data structure to server:
{
"boolean": true,
"stringBoolean": "true",
"number": 213,
"stringNubmer": "44444444",
"string": "adssd",
"arrayNumber": [1, 2, 3, 4],
"arrayBoolean": [true, false, "true", "false"]
}
but this time I have numbers and booleans in console:
{ boolean: true,
stringBoolean: 'true',
number: 213,
stringNubmer: '44444444',
string: 'adssd',
arrayNumber: [ 1, 2, 3, 4 ],
arrayBoolean: [ true, false, 'true', 'false' ] }
Seems like angular doing some conventions behind the curtains. How can I avoid that and get correct values types send to server?
UPDATE:
In chome console I can see request body in raw format wich look like this:
boolean=true&stringBoolean=true&number=213&stringNubmer=44444444&string=adssd&arrayNumber%5B0%5D=1&arrayNumber%5B1%5D=2&arrayNumber%5B2%5D=3&arrayNumber%5B3%5D=4&arrayBoolean%5B0%5D=true&arrayBoolean%5B1%5D=false&arrayBoolean%5B2%5D=true&arrayBoolean%5B3%5D=false
which mean each value will always be string and I have to manually convert it on server side to appropriate types. Is this questing right? What are the good practices here?
it is using JSON.stringify() internally. use JSON.parse()

How to count only particular entries in json data

I have this sample json data:
{
"sessionsData" :[{
"isActive": true,
"isInactive": false,
"isScheduled": false,
"isExpired": false,
"candidateName":"Payal Singh",
"email": "ggera#sapient.com"
},
{
"isActive": false,
"isInactive": true,
"isScheduled": false,
"isExpired": false,
"candidateName":"Shyam Singh",
"email": "ggera#sapient.com"
},
{
"isActive": false,
"isInactive": false,
"isScheduled": true,
"isExpired": false,
"candidateName":"Payal Singh",
"email": "ggera#sapient.com"
},
{
"isActive": false,
"isInactive": false,
"isScheduled": false,
"isExpired": true,
"candidateName":"Payal Singh",
"email": "ggera#sapient.com"
}]
}
Now I want to count only the entries which have "isActive" property set to true by using angular.
Anyone have any suggestions?
arr.sessionsData.filter(function(obj,key){
return obj.isActive==true
})
OR:
ECMA6 :
var arrFiltered = arr.sessionsData.filter((obj,key)=>
obj.isActive==true
)
https://docs.angularjs.org/api/ng/filter/filter. Follow this link, it is very detailed.I went around it like this:
$scope.activeEntries = $filter('filter')($scope.session.sessionsData, {isActive: true});
where $scope.session.sessionsData is the array in which I retrieved the json data.

Set criteria in query for fields and fields in nested objects

I have a document like this:
{
"InDate": "11.09.2015",
"Kst2Kst": true,
"OutDate": "11.09.2015",
"__v": 0,
"_id": ObjectId('55f2df2d7e12a9f1f52837e6'),
"accepted": true,
"inventar": [
{
"accepted": "1",
"name": "AAAA",
"isstammkost": true,
"stammkost": "IWXI"
},
{
"accepted": "1",
"name": "BBBB",
"isstammkost": false,
"stammkost": "null"
}
]
}
I want to select the data with "isstammkost": true in the inventar-array.
My query is:
Move.findOne({accepted : true, 'inventar.isstammkost' : true},
'OutDate InDate inventar.name', function(err, res)
It doesn't work -> It selects all, even with inventar.isstammkost : false.
The "normal" query works like I want (without criteria in sub-array). Whats the right way to set criteria in sub-array?
Of course it will return the "isstammkost": false part, because that is part of the same document as the "isstammkost": true. They are both objects in the array "inventar", a top-level field in a single document. Without some sort of projection, the entire document will always be returned to a mongodb query and thus nodejs will pass them on to you.
I'm not terribly up-to-speed on nodejs, but if this were the mongo shell it would look like this:
> db.MyDB.findOne({{accepted : true, "inventar.isstammkost" : true}, {"inventar.isstammkost.$": 1});
You will need to find out how to add that extra parameter to the nodejs function.

Test if a value exists in a map in a ng-if

How can I test if a value exists in a map in a ng-if?
$scope.textInputTypes = {
"currency": true,
"double": true,
"percent": true,
"int": true,
"email": true,
"phone": true,
"string": true,
"textarea": true,
"url": true
};
ng-if='mytype in textInputTypes'
This gives me an error:
Syntax Error: Token 'in' is an unexpected token at column 17 of the
expression [mytypeNaNn textInputTypes] starting at [in
textInputTypes].
You can also do the following:
<element ng-if='textInputTypes.hasOwnProperty(mytype)'>
Just keep in mind that the content inside the element won't be part of the DOM if the IF condition is not met, if you still want the content to exist you can also use ng-show
Finally, I found a way to do it.
ng-if="textInputTypes[myType]"

Datatables.responsive.js crashes when Datatable has no rows

As per title, I have a Datatable, that loads data using the ajax options. This works fine.
When I add the responsive extension Version 1.01, it works as expected when I resize the browser EXCEPT when my datatable has no rows.
datatable.responsive.js crashes at line 561:
var clone = dt.row( idx ).node().cloneNode( true );
with error:
Microsoft JScript runtime error: 'dt.row(...).node()' is null or not an object
I am simply adding the line:
responsive: true,
to my Datatable defintion as follows:
$('#tableName').dataTable({
processing: true,
pagingType: "full_numbers",
serverSide: true,
responsive: true,
columns: [
{ name: "ID" },
{ name: "ColumnName1" },
...more columns
],
ajax: "MyURL"
});
});
This has been fixed as part of this release

Resources