Code


Version 5 (modified by hugo <gb@…>, 9 years ago) (diff)

--

Common Pitfalls - Data Models

Foreign Keys

the 'edit_inline' option

Warning

As mentioned elsewhere in the documentation, if you are using edit_inline in data model's foreign key, be sure the rest of your fields use the option core=True. Failure to do so will lead to obscure errors or no errors.

Symptoms

Errors like the following:

AttributeError: 'User' object has no attribute 'get_message_list'
ProgrammingError: ERROR:  invalid input syntax for integer: ""

Image/File Fields

providing an image field in without media configured

Warning

If you are going to have an image field in your data model, you have to have a directory configured where the images will be saved. The entry in the database for the image field is a string of the filesystem path where the image is/will be saved.

Symptoms

Data not being posted to the database with a refresh of the "add" screen.

Using Module Globals in Model Classes

The 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:

import md5

class Something(meta.Model):

    def func(self, parm):
          md5.new()

Warning

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 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:

import md5

class Something(meta.Model):

    module_constants = {
        'md5': md5
    }

    def func(self, parm):
          md5.new()

Read the article this is based on in full over here.

Symptoms

You get tracebacks with name errors in your modules as if the global wasn't there.