in a quickly accessible way. Whereas <aclass="reference internal"href="../Components/Tags.html"><spanclass="doc std std-doc">Tags</span></a> or <aclass="reference internal"href="../Components/Scripts.html"><spanclass="doc std std-doc">Scripts</span></a> can handle many situations,
<li><p>A new folder <codeclass="docutils literal notranslate"><spanclass="pre">myapp</span></code> is created. “myapp” will also be the name (the “app label”) from now on. We
chose to put it in the <codeclass="docutils literal notranslate"><spanclass="pre">world/</span></code> subfolder here, but you could put it in the root of your <codeclass="docutils literal notranslate"><spanclass="pre">mygame</span></code> if
interested in for now is <codeclass="docutils literal notranslate"><spanclass="pre">models.py</span></code>. In <codeclass="docutils literal notranslate"><spanclass="pre">models.py</span></code> you define your model(s). Each model will be a
table in the database. See the next section and don’t continue until you have added the models you
want.</p></li>
<li><p>You now need to tell Evennia that the models of your app should be a part of your database
scheme. Add this line to your <codeclass="docutils literal notranslate"><spanclass="pre">mygame/server/conf/settings.py</span></code>file (make sure to use the path where
you put <codeclass="docutils literal notranslate"><spanclass="pre">myapp</span></code> and don’t forget the comma at the end of the tuple):</p>
control (if not, <aclass="reference internal"href="../Coding/Version-Control.html"><spanclass="doc std std-doc">you should</span></a>), don’t forget to <codeclass="docutils literal notranslate"><spanclass="pre">git</span><spanclass="pre">add</span><spanclass="pre">myapp/*</span></code> to add all items
<p>A Django <em>model</em> is the Python representation of a database table. It can be handled like any other
Python class. It defines <em>fields</em> on itself, objects of a special type. These become the “columns”
of the database table. Finally, you create new instances of the model to add new rows to the
database.</p>
<p>We won’t describe all aspects of Django models here, for that we refer to the vast <aclass="reference external"href="https://docs.djangoproject.com/en/2.2/topics/db/models/">Django
<div><p>The <codeclass="docutils literal notranslate"><spanclass="pre">db_date_created</span></code> field, with exactly this name, is <em>required</em> if you want to be able to store
instances of your custom model in an Evennia <aclass="reference internal"href="../Components/Attributes.html"><spanclass="doc std std-doc">Attribute</span></a>. It will automatically be set
upon creation and can after that not be changed. Having this field will allow you to do e.g.
<codeclass="docutils literal notranslate"><spanclass="pre">obj.db.myinstance</span><spanclass="pre">=</span><spanclass="pre">mydatastore</span></code>. If you know you’ll never store your model instances in Attributes
the <codeclass="docutils literal notranslate"><spanclass="pre">db_date_created</span></code> field is optional.</p>
<p>You don’t <em>have</em> to start field names with <codeclass="docutils literal notranslate"><spanclass="pre">db_</span></code>, this is an Evennia convention. It’s nevertheless
recommended that you do use <codeclass="docutils literal notranslate"><spanclass="pre">db_</span></code>, partly for clarity and consistency with Evennia (if you ever want
to share your code) and partly for the case of you later deciding to use Evennia’s
<codeclass="docutils literal notranslate"><spanclass="pre">SharedMemoryModel</span></code> parent down the line.</p>
<p>The field keyword <codeclass="docutils literal notranslate"><spanclass="pre">db_index</span></code> creates a <em>database index</em> for this field, which allows quicker
lookups, so it’s recommended to put it on fields you know you’ll often use in queries. The
<codeclass="docutils literal notranslate"><spanclass="pre">null=True</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">blank=True</span></code> keywords means that these fields may be left empty or set to the empty
string without the database complaining. There are many other field types and keywords to define
them, see django docs for more info.</p>
<p>Similar to using <aclass="reference external"href="https://docs.djangoproject.com/en/2.2/howto/legacy-databases/">django-admin</a> you
are able to do <codeclass="docutils literal notranslate"><spanclass="pre">evennia</span><spanclass="pre">inspectdb</span></code> to get an automated listing of model information for an existing
database. As is the case with any model generating tool you should only use this as a starting
<h2>Creating a new model instance<aclass="headerlink"href="#creating-a-new-model-instance"title="Permalink to this headline">¶</a></h2>
<p>To create a new row in your table, you instantiate the model and then call its <codeclass="docutils literal notranslate"><spanclass="pre">save()</span></code> method:</p>
<p>Note that the <codeclass="docutils literal notranslate"><spanclass="pre">db_date_created</span></code> field of the model is not specified. Its flag <codeclass="docutils literal notranslate"><spanclass="pre">at_now_add=True</span></code>
makes sure to set it to the current date when the object is created (it can also not be changed
further after creation).</p>
<p>When you update an existing object with some new field value, remember that you have to save the
object afterwards, otherwise the database will not update:</p>
<p>Evennia’s normal models don’t need to explicitly save, since they are based on <codeclass="docutils literal notranslate"><spanclass="pre">SharedMemoryModel</span></code>
rather than the raw django model. This is covered in the next section.</p>
<h2>Using the <codeclass="docutils literal notranslate"><spanclass="pre">SharedMemoryModel</span></code> parent<aclass="headerlink"href="#using-the-sharedmemorymodel-parent"title="Permalink to this headline">¶</a></h2>
<p>Evennia doesn’t base most of its models on the raw <codeclass="docutils literal notranslate"><spanclass="pre">django.db.models</span></code> but on the Evennia base model
<codeclass="docutils literal notranslate"><spanclass="pre">evennia.utils.idmapper.models.SharedMemoryModel</span></code>. There are two main reasons for this:</p>
<li><p>Ease of updating fields without having to explicitly call <codeclass="docutils literal notranslate"><spanclass="pre">save()</span></code></p></li>
<li><p>On-object memory persistence and database caching</p></li>
<p>The first (and least important) point means that as long as you named your fields <codeclass="docutils literal notranslate"><spanclass="pre">db_*</span></code>, Evennia
will automatically create field wrappers for them. This happens in the model’s
<aclass="reference external"href="http://en.wikibooks.org/wiki/Python_Programming/Metaclasses">Metaclass</a> so there is no speed
penalty for this. The name of the wrapper will be the same name as the field, minus the <codeclass="docutils literal notranslate"><spanclass="pre">db_</span></code>
prefix. So the <codeclass="docutils literal notranslate"><spanclass="pre">db_key</span></code> field will have a wrapper property named <codeclass="docutils literal notranslate"><spanclass="pre">key</span></code>. You can then do:</p>
<p>and don’t have to explicitly call <codeclass="docutils literal notranslate"><spanclass="pre">save()</span></code> afterwards. The saving also happens in a more efficient
way under the hood, updating only the field rather than the entire model using django optimizations.
Note that if you were to manually add the property or method <codeclass="docutils literal notranslate"><spanclass="pre">key</span></code> to your model, this will be used
instead of the automatic wrapper and allows you to fully customize access as needed.</p>
<p>To explain the second and more important point, consider the following example using the default
<spanclass="n">shield</span><spanclass="o">.</span><spanclass="n">cracked</span><spanclass="o">=</span><spanclass="kc">True</span><spanclass="c1"># where cracked is not a database field</span>
<p>The outcome of that last print statement is <em>undefined</em>! It could <em>maybe</em> randomly work but most
likely you will get an <codeclass="docutils literal notranslate"><spanclass="pre">AttributeError</span></code> for not finding the <codeclass="docutils literal notranslate"><spanclass="pre">cracked</span></code> property. The reason is that
<codeclass="docutils literal notranslate"><spanclass="pre">cracked</span></code> doesn’t represent an actual field in the database. It was just added at run-time and thus
Django don’t care about it. When you retrieve your shield-match later there is <em>no</em> guarantee you
will get back the <em>same Python instance</em> of the model where you defined <codeclass="docutils literal notranslate"><spanclass="pre">cracked</span></code>, even if you
search for the same database object.</p>
<p>Evennia relies heavily on on-model handlers and other dynamically created properties. So rather than
using the vanilla Django models, Evennia uses <codeclass="docutils literal notranslate"><spanclass="pre">SharedMemoryModel</span></code>, which levies something called
<em>idmapper</em>. The idmapper caches model instances so that we will always get the <em>same</em> instance back
after the first lookup of a given object. Using idmapper, the above example would work fine and you
could retrieve your <codeclass="docutils literal notranslate"><spanclass="pre">cracked</span></code> property at any time - until you rebooted when all non-persistent data
goes.</p>
<p>Using the idmapper is both more intuitive and more efficient <em>per object</em>; it leads to a lot less
reading from disk. The drawback is that this system tends to be more memory hungry <em>overall</em>. So if
you know that you’ll <em>never</em> need to add new properties to running instances or know that you will
create new objects all the time yet rarely access them again (like for a log system), you are
probably better off making “plain” Django models rather than using <codeclass="docutils literal notranslate"><spanclass="pre">SharedMemoryModel</span></code> and its
idmapper.</p>
<p>To use the idmapper and the field-wrapper functionality you just have to have your model classes
inherit from <codeclass="docutils literal notranslate"><spanclass="pre">evennia.utils.idmapper.models.SharedMemoryModel</span></code> instead of from the default
<p>To search your new custom database table you need to use its database <em>manager</em> to build a <em>query</em>.
Note that even if you use <codeclass="docutils literal notranslate"><spanclass="pre">SharedMemoryModel</span></code> as described in the previous section, you have to use
the actual <em>field names</em> in the query, not the wrapper name (so <codeclass="docutils literal notranslate"><spanclass="pre">db_key</span></code> and not just <codeclass="docutils literal notranslate"><spanclass="pre">key</span></code>).</p>