Commit 977cfb6f authored by Administrator's avatar Administrator

tutorial with node orm

parent 3818a96f
from django.contrib.auth.models import User
from django.db import models
from django.utils import timezone
from django_hstore import hstore
from treebeard.mp_tree import MP_Node
######################################################################
# DATABASES / LANGUAGES
......
......@@ -61,6 +61,7 @@ INSTALLED_APPS = (
'django_extensions',
#'south',
'documents',
'node',
'django_hstore',
)
......
{
"metadata": {
"name": "",
"signature": "sha256:fd8cdda63e0e9cba7dbdfac864550d69bace0f3f834ebd216402a50bc10992b1"
"signature": "sha256:8b4bca4e664f5f739f7bce44d851aba74094e40df7002644ba6d0a621b25f52a"
},
"nbformat": 3,
"nbformat_minor": 0,
......@@ -12,37 +12,131 @@
"cell_type": "code",
"collapsed": false,
"input": [
"cd .."
"from node.models import Node"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"root = Node.add_root(name=\"Projet 5\", user_id=1, type_id=1)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 39
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"root.save()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 40
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"s = root.get_siblings()\n",
"s"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"metadata": {},
"output_type": "pyout",
"prompt_number": 41,
"text": [
"/home/alexandre/projets/gargantext.py\n"
"[<Node: Projet 5>, <Node: Projet 4>, <Node: Projet 4>, <Node: Projet 3>, <Node: Projet 2>, <Node: Projet 1>]"
]
}
],
"prompt_number": 1
"prompt_number": 41
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import gargantext_core as gargantext"
"r = s[0]\n",
"r"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 43,
"text": [
"<Node: Projet 1>"
]
}
],
"prompt_number": 43
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"r.add_child(name=\"Corpus Science\", user_id=1, type_id=1)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 44,
"text": [
"<Node: Corpus Science>"
]
}
],
"prompt_number": 44
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"science.save()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"cd gargantext_web/"
"r = s"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"help(root.get_siblings)"
],
"language": "python",
"metadata": {},
......@@ -51,22 +145,294 @@
"output_type": "stream",
"stream": "stdout",
"text": [
"/home/alexandre/projets/gargantext.py/gargantext_web\n"
"Help on method get_siblings in module treebeard.ns_tree:\n",
"\n",
"get_siblings() method of node.models.Node instance\n",
" :returns: A queryset of all the node's siblings, including the node\n",
" itself.\n",
"\n"
]
}
],
"prompt_number": 3
"prompt_number": 32
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import documents"
"n = s[0]\n",
"n.get_parent()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
"prompt_number": 22
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Node.get_root_nodes()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 28,
"text": [
"[<Node: Projet 3>, <Node: Projet 2>, <Node: Projet 1>]"
]
}
],
"prompt_number": 28
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"help(Node.get_root_nodes())"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Help on HStoreQuerySet in module django_hstore.query object:\n",
"\n",
"class HStoreQuerySet(django.db.models.query.QuerySet)\n",
" | Method resolution order:\n",
" | HStoreQuerySet\n",
" | django.db.models.query.QuerySet\n",
" | builtins.object\n",
" | \n",
" | Methods defined here:\n",
" | \n",
" | __init__(self, model=None, query=None, using=None, *args, **kwargs)\n",
" | \n",
" | hkeys = selector(self, *args, **params)\n",
" | \n",
" | hpeek = selector(self, *args, **params)\n",
" | \n",
" | hremove = updater(self, *args, **params)\n",
" | \n",
" | hslice = selector(self, *args, **params)\n",
" | \n",
" | hupdate = updater(self, *args, **params)\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Methods inherited from django.db.models.query.QuerySet:\n",
" | \n",
" | __and__(self, other)\n",
" | \n",
" | __deepcopy__(self, memo)\n",
" | Deep copy of a QuerySet doesn't populate the cache\n",
" | \n",
" | __getitem__(self, k)\n",
" | Retrieves an item or slice from the set of results.\n",
" | \n",
" | __getstate__(self)\n",
" | Allows the QuerySet to be pickled.\n",
" | \n",
" | __iter__(self)\n",
" | The queryset iterator protocol uses three nested iterators in the\n",
" | default case:\n",
" | 1. sql.compiler:execute_sql()\n",
" | - Returns 100 rows at time (constants.GET_ITERATOR_CHUNK_SIZE)\n",
" | using cursor.fetchmany(). This part is responsible for\n",
" | doing some column masking, and returning the rows in chunks.\n",
" | 2. sql/compiler.results_iter()\n",
" | - Returns one row at time. At this point the rows are still just\n",
" | tuples. In some cases the return values are converted to\n",
" | Python values at this location (see resolve_columns(),\n",
" | resolve_aggregate()).\n",
" | 3. self.iterator()\n",
" | - Responsible for turning the rows into model objects.\n",
" | \n",
" | __len__(self)\n",
" | \n",
" | __nonzero__(self)\n",
" | \n",
" | __or__(self, other)\n",
" | \n",
" | __repr__(self)\n",
" | \n",
" | aggregate(self, *args, **kwargs)\n",
" | Returns a dictionary containing the calculations (aggregation)\n",
" | over the current queryset\n",
" | \n",
" | If args is present the expression is passed as a kwarg using\n",
" | the Aggregate object's default alias.\n",
" | \n",
" | all(self)\n",
" | Returns a new QuerySet that is a copy of the current one. This allows a\n",
" | QuerySet to proxy for a model manager in some cases.\n",
" | \n",
" | annotate(self, *args, **kwargs)\n",
" | Return a query set in which the returned objects have been annotated\n",
" | with data aggregated from related fields.\n",
" | \n",
" | bulk_create(self, objs, batch_size=None)\n",
" | Inserts each of the instances into the database. This does *not* call\n",
" | save() on each of the instances, does not send any pre/post save\n",
" | signals, and does not set the primary key attribute if it is an\n",
" | autoincrement field.\n",
" | \n",
" | complex_filter(self, filter_obj)\n",
" | Returns a new QuerySet instance with filter_obj added to the filters.\n",
" | \n",
" | filter_obj can be a Q object (or anything with an add_to_query()\n",
" | method) or a dictionary of keyword lookup arguments.\n",
" | \n",
" | This exists to support framework features such as 'limit_choices_to',\n",
" | and usually it will be more natural to use other methods.\n",
" | \n",
" | count(self)\n",
" | Performs a SELECT COUNT() and returns the number of records as an\n",
" | integer.\n",
" | \n",
" | If the QuerySet is already fully cached this simply returns the length\n",
" | of the cached results set to avoid multiple SELECT COUNT(*) calls.\n",
" | \n",
" | create(self, **kwargs)\n",
" | Creates a new object with the given kwargs, saving it to the database\n",
" | and returning the created object.\n",
" | \n",
" | dates(self, field_name, kind, order='ASC')\n",
" | Returns a list of date objects representing all available dates for\n",
" | the given field_name, scoped to 'kind'.\n",
" | \n",
" | datetimes(self, field_name, kind, order='ASC', tzinfo=None)\n",
" | Returns a list of datetime objects representing all available\n",
" | datetimes for the given field_name, scoped to 'kind'.\n",
" | \n",
" | defer(self, *fields)\n",
" | Defers the loading of data for certain fields until they are accessed.\n",
" | The set of fields to defer is added to any existing set of deferred\n",
" | fields. The only exception to this is if None is passed in as the only\n",
" | parameter, in which case all deferrals are removed (None acts as a\n",
" | reset option).\n",
" | \n",
" | delete(self)\n",
" | Deletes the records in the current QuerySet.\n",
" | \n",
" | distinct(self, *field_names)\n",
" | Returns a new QuerySet instance that will select only distinct results.\n",
" | \n",
" | earliest(self, field_name=None)\n",
" | \n",
" | exclude(self, *args, **kwargs)\n",
" | Returns a new QuerySet instance with NOT (args) ANDed to the existing\n",
" | set.\n",
" | \n",
" | exists(self)\n",
" | \n",
" | extra(self, select=None, where=None, params=None, tables=None, order_by=None, select_params=None)\n",
" | Adds extra SQL fragments to the query.\n",
" | \n",
" | filter(self, *args, **kwargs)\n",
" | Returns a new QuerySet instance with the args ANDed to the existing\n",
" | set.\n",
" | \n",
" | first(self)\n",
" | Returns the first object of a query, returns None if no match is found.\n",
" | \n",
" | get(self, *args, **kwargs)\n",
" | Performs the query and returns a single object matching the given\n",
" | keyword arguments.\n",
" | \n",
" | get_or_create(self, **kwargs)\n",
" | Looks up an object with the given kwargs, creating one if necessary.\n",
" | Returns a tuple of (object, created), where created is a boolean\n",
" | specifying whether an object was created.\n",
" | \n",
" | in_bulk(self, id_list)\n",
" | Returns a dictionary mapping each of the given IDs to the object with\n",
" | that ID.\n",
" | \n",
" | iterator(self)\n",
" | An iterator over the results from applying this QuerySet to the\n",
" | database.\n",
" | \n",
" | last(self)\n",
" | Returns the last object of a query, returns None if no match is found.\n",
" | \n",
" | latest(self, field_name=None)\n",
" | \n",
" | none(self)\n",
" | Returns an empty QuerySet.\n",
" | \n",
" | only(self, *fields)\n",
" | Essentially, the opposite of defer. Only the fields passed into this\n",
" | method and that are not already specified as deferred are loaded\n",
" | immediately when the queryset is evaluated.\n",
" | \n",
" | order_by(self, *field_names)\n",
" | Returns a new QuerySet instance with the ordering changed.\n",
" | \n",
" | prefetch_related(self, *lookups)\n",
" | Returns a new QuerySet instance that will prefetch the specified\n",
" | Many-To-One and Many-To-Many related objects when the QuerySet is\n",
" | evaluated.\n",
" | \n",
" | When prefetch_related() is called more than once, the list of lookups to\n",
" | prefetch is appended to. If prefetch_related(None) is called, the\n",
" | the list is cleared.\n",
" | \n",
" | reverse(self)\n",
" | Reverses the ordering of the QuerySet.\n",
" | \n",
" | select_for_update(self, **kwargs)\n",
" | Returns a new QuerySet instance that will select objects with a\n",
" | FOR UPDATE lock.\n",
" | \n",
" | select_related(self, *fields, **kwargs)\n",
" | Returns a new QuerySet instance that will select related objects.\n",
" | \n",
" | If fields are specified, they must be ForeignKey fields and only those\n",
" | related objects are included in the selection.\n",
" | \n",
" | If select_related(None) is called, the list is cleared.\n",
" | \n",
" | update(self, **kwargs)\n",
" | Updates all elements in the current QuerySet, setting all the given\n",
" | fields to the appropriate values.\n",
" | \n",
" | using(self, alias)\n",
" | Selects which database this QuerySet should excecute its query against.\n",
" | \n",
" | values(self, *fields)\n",
" | \n",
" | values_list(self, *fields, **kwargs)\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Data descriptors inherited from django.db.models.query.QuerySet:\n",
" | \n",
" | __dict__\n",
" | dictionary for instance variables (if defined)\n",
" | \n",
" | __weakref__\n",
" | list of weak references to the object (if defined)\n",
" | \n",
" | db\n",
" | Return the database that will be used if this query is executed now\n",
" | \n",
" | ordered\n",
" | Returns True if the QuerySet is ordered -- i.e. has an order_by()\n",
" | clause or a default ordering on the model.\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Data and other attributes inherited from django.db.models.query.QuerySet:\n",
" | \n",
" | value_annotation = True\n",
"\n"
]
}
],
"prompt_number": 29
},
{
"cell_type": "code",
......
......@@ -62,8 +62,6 @@
<script src="{% static "js/jquery-1.9.1.min.js"%}"></script>
<script src="{% static "js/easy/jquery.easypiechart.js"%}"></script>
<script type="text/javascript">
......@@ -76,7 +74,6 @@ $(function() {
</script>
<script src="{% static "js/bootstrap.min.js" %}"></script>
</body>
</html>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment