Changes between Version 4 and Version 5 of DataModelPitfalls


Ignore:
Timestamp:
08/09/2005 04:55:35 PM (10 years ago)
Author:
hugo <gb@…>
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DataModelPitfalls

    v4 v5  
    2424Data not being posted to the database with a refresh of the "add" screen.
    2525
     26== Using Module Globals in Model Classes ==
     27
     28The model module can import stuff or define globals. You might want to use those globals or imported modules in your models classes methods like this:
     29
     30{{{
     31import md5
     32
     33class Something(meta.Model):
     34
     35    def func(self, parm):
     36          md5.new()
     37}}}
     38
     39==== Warning ====
     40
     41If 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 is 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:
     42
     43{{{
     44import md5
     45
     46class Something(meta.Model):
     47
     48    module_constants = {
     49        'md5': md5
     50    }
     51
     52    def func(self, parm):
     53          md5.new()
     54}}}
     55
     56Read the article this is based on in full [http://www.deadlybloodyserious.com/categories/django/2005/08/07.html#P85 over here].
     57
     58==== Symptoms ====
     59
     60You get tracebacks with name errors in your modules as if the global wasn't there.
Back to Top