Code

Changes between Version 14 and Version 15 of DataModelPitfalls


Ignore:
Timestamp:
05/12/06 00:31:35 (8 years ago)
Author:
ubernostrum
Comment:

details on importing/globals, moving it up since it's an extremely common question

Legend:

Unmodified
Added
Removed
Modified
  • DataModelPitfalls

    v14 v15  
    22 
    33If you encounter a strange error, make sure to run {{{django-admin.py validate}}}. That catches some common errors in model syntax. 
     4 
     5== Using module globals in models == 
     6 
     7The model module, being a Python module, can import modules or define globals. You might want to use those globals or imported modules in your models classes methods like this: 
     8 
     9{{{ 
     10import md5 
     11 
     12class Something(meta.Model): 
     13    def func(self, parm): 
     14        md5.new() 
     15}}} 
     16 
     17==== Warning ==== 
     18 
     19In Django 0.90 and Django 0.91, and in Django trunk up until the merge of the magic-removal branch, you may encounter problems with importing modules or defining global variables in a model file, because those versions of Django create dynamic model classes at runtime which do not get the global scope of the original model file. To work around this, give your model a 'module_constants' dictionary like so: 
     20 
     21{{{ 
     22import md5 
     23 
     24class Something(meta.Model): 
     25    class META: 
     26        module_constants = { 
     27            'md5': md5 
     28        } 
     29    def func(self, parm): 
     30          md5.new() 
     31}}} 
     32 
     33Or, simply import the modules needed in the function itself: 
     34 
     35{{{ 
     36class Something(meta.Model): 
     37    ... 
     38    def func(self): 
     39        import md5 
     40        md5.new(...) 
     41}}} 
     42 
     43This problem can also be avoided by using a more recent version of Django (i.e., any trunk version based on the magic-removal codebase). 
     44 
     45[http://www.deadlybloodyserious.com/categories/django/2005/08/07.html#P85 More information]. 
     46 
     47==== Symptoms ==== 
     48 
     49You get tracebacks with {{{NameError}}}s in your modules as if the global wasn't there. 
     50 
    451 
    552== !ManyToManyField == 
     
    2269Data not being posted to the database with a refresh of the "add" screen. 
    2370 
    24 == Using module globals in models == 
    25  
    26 The model module, being a Python module, can import stuff or define globals. You might want to use those globals or imported modules in your models classes methods like this: 
    27  
    28 {{{ 
    29 import md5 
    30  
    31 class Something(meta.Model): 
    32     def func(self, parm): 
    33         md5.new() 
    34 }}} 
    35  
    36 ==== Warning ==== 
    37  
    38 If you import modules in your model files, or if you define globals in your model file and want to use them in methods of the model classes, you need to define module_constants and pass along those globals you wish to use in the model. This happens because every model class is turned into a virtual module that doesn't automatically inherit all global values from your model file. Do it like this: 
    39  
    40 {{{ 
    41 import md5 
    42  
    43 class Something(meta.Model): 
    44     class META: 
    45         module_constants = { 
    46             'md5': md5 
    47         } 
    48     def func(self, parm): 
    49           md5.new() 
    50 }}} 
    51  
    52 Or, you can simply import the modules needed in the function itself: 
    53  
    54 {{{ 
    55 class Something(meta.Model): 
    56     ... 
    57     def func(self): 
    58         import md5 
    59         md5.new(...) 
    60 }}} 
    61  
    62 [http://www.deadlybloodyserious.com/categories/django/2005/08/07.html#P85 More information]. 
    63  
    64 ==== Symptoms ==== 
    65  
    66 You get tracebacks with {{{NameError}}}s in your modules as if the global wasn't there. 
    67  
    68  
    6971== If You Find get_X_list, get_X_count, etc. Not Getting Created == 
    7072