Changes between Version 7 and Version 8 of DataModelPitfalls


Ignore:
Timestamp:
08/15/2005 12:37:52 PM (10 years ago)
Author:
adrian
Comment:

Cleaned up DataModelPitfalls

Legend:

Unmodified
Added
Removed
Modified
  • DataModelPitfalls

    v7 v8  
    11= Common Pitfalls - Data Models =
     2
     3If you encounter a strange error, make sure to run {{{django-admin.py validate}}}. That catches some common errors in model syntax.
    24
    35== ManyToManyField ==
    46
    5 === name likeness of helper classes and field classes ===
     7=== Name likeness of helper classes and field classes ===
    68
    79==== Warning ====
    8 The field class is ManyToManyField, not ManyToMany - the latter is just the class of the relation that is stored with the field, in the model you need to use the field class.
     10The field class is {{{ManyToManyField}}}, not {{{ManyToMany}}}. The latter is for internal use only.
    911
    1012==== Symptoms ====
    11 A traceback that talks about __init__ getting 2 instead of 3 required positional parameters in the line of your model where the fields tuple is built.
     13A traceback that talks about {{{__init__}}} getting 2 instead of 3 required positional parameters in the line of your model where the fields tuple is built.
    1214
    13 == Foreign Keys ==
     15== ImageFields/FileFields ==
    1416
    15 === the 'edit_inline' option ===
     17=== Providing an image field in without media configured ===
    1618==== Warning ====
    17 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.
    18 
    19 ==== Symptoms ====
    20 Errors like the following:
    21 {{{
    22 AttributeError: 'User' object has no attribute 'get_message_list'
    23 ProgrammingError: ERROR:  invalid input syntax for integer: ""
    24 }}}
    25 
    26 
    27 == Image/File Fields ==
    28 
    29 === providing an image field in without media configured ===
    30 ==== Warning ====
    31 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.
     19If you have an ImageField in your model, you need to configure media. See [http://www.djangoproject.com/documentation/faq/#how-do-i-use-image-and-file-fields How do I use image and file fields?] in the FAQ.
    3220
    3321==== Symptoms ====
    3422Data not being posted to the database with a refresh of the "add" screen.
    3523
    36 == Using Module Globals in Model Classes ==
     24== Using module globals in models ==
    3725
    38 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:
     26The 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:
    3927
    4028{{{
     
    4230
    4331class Something(meta.Model):
    44 
    4532    def func(self, parm):
    46           md5.new()
     33        md5.new()
    4734}}}
    4835
    4936==== Warning ====
    5037
    51 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:
     38If 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:
    5239
    5340{{{
     
    5542
    5643class Something(meta.Model):
    57 
    5844    module_constants = {
    5945        'md5': md5
    6046    }
    61 
    6247    def func(self, parm):
    6348          md5.new()
     
    6954class Something(meta.Model):
    7055    ...
    71 
    7256    def func(self):
    7357        import md5
     
    7559}}}
    7660
    77 Read the article this is based on in full [http://www.deadlybloodyserious.com/categories/django/2005/08/07.html#P85 over here].
     61[http://www.deadlybloodyserious.com/categories/django/2005/08/07.html#P85 More information].
    7862
    7963==== Symptoms ====
    8064
    81 You get tracebacks with name errors in your modules as if the global wasn't there.
     65You get tracebacks with {{{NameError}}}s in your modules as if the global wasn't there.
Back to Top