Django

Code

Changeset 2755

Show
Ignore:
Timestamp:
04/27/06 17:12:05 (2 years ago)
Author:
adrian
Message:

magic-removal: Added 'Field name restrictions' section placeholder to docs/model-api.txt

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/magic-removal/docs/model-api.txt

    r2708 r2755  
    2222 
    2323The most important part of a model is the list of database fields it defines. 
    24 Fields are defined by class attributes.  
     24Fields are defined by class attributes. 
    2525 
    2626In this example, there are two fields, ``first_name`` and ``last_name`` :: 
     
    5656Convention is not to capitalize the first letter of the ``verbose_name``. 
    5757Django will automatically capitalize the first letter where it needs to. 
     58 
     59Field name restrictions 
     60----------------------- 
     61 
     62TODO: Fill this section. 
     63 
     64    * Can't be Python reserved word. 
     65    * Can't have more than one underscore in a row, due to query lookup. 
    5866 
    5967General field options 
     
    492500 
    493501If you need to create a relationship on a model that has not yet been defined, 
    494 you can use the name of the model, rather than the model object itself::  
     502you can use the name of the model, rather than the model object itself:: 
    495503 
    496504    class Place(models.Model): 
     
    502510 
    503511The name of a ``ForeignKey`` (``city`` in the example above) generally should 
    504 be the name of the model, in singular form. Behind the scenes, Django appends  
    505 "_id" to the field name to create its database column name. However, your code  
    506 should never have to deal with the database column name, unless you write  
     512be the name of the model, in singular form. Behind the scenes, Django appends 
     513"_id" to the field name to create its database column name. However, your code 
     514should never have to deal with the database column name, unless you write 
    507515custom SQL. 
    508516 
     
    599607 
    600608                             ...which would give ``Category`` objects two Object Set 
    601                              descriptors - one called ``primary_stories`` and one  
     609                             descriptors - one called ``primary_stories`` and one 
    602610                             called ``secondary_stories``. 
    603611 
     
    679687                             parameter to change this, which is if you want one model to 
    680688                             have multiple ``ManyToMany`` relationships to another model. 
    681                               
     689 
    682690    ``symmetrical``          Only used in the definition of ManyToManyFields on self. 
    683691                             Consider the following model: 
    684                               
     692 
    685693                             class Person(models.Model): 
    686694                                 friends = models.ManyToManyField("self") 
    687                               
     695 
    688696                             When Django processes this model, it identifies that it has 
    689697                             a ManyToManyField on itself, and as a result, it doesn't add 
    690698                             a ``person_set`` attribute to the Person class. Instead, the 
    691                              ManyToManyField is assumed to be symmetrical - that is, if  
    692                              I am your friend, then you are my friend.  
    693                               
    694                              If you do not want symmetry in ManyToMany relationships with  
    695                              self, set ``symmetrical`` to False. This will force Django to  
    696                              add the descriptor for the reverse relationship, allow  
    697                              ManyToMany relationships to be non-symmetrical.  
    698                               
     699                             ManyToManyField is assumed to be symmetrical - that is, if 
     700                             I am your friend, then you are my friend. 
     701 
     702                             If you do not want symmetry in ManyToMany relationships with 
     703                             self, set ``symmetrical`` to False. This will force Django to 
     704                             add the descriptor for the reverse relationship, allow 
     705                             ManyToMany relationships to be non-symmetrical. 
     706 
    699707    =======================  ============================================================ 
    700708 
     
    710718 
    711719``OneToOneField`` requires a positional argument: The class to which the 
    712 model is related.  
     720model is related. 
    713721 
    714722For example, if you're building a database of "places", you would build pretty 
     
    861869============= 
    862870 
    863 If you want your model to be visible to the automatic Administration  
     871If you want your model to be visible to the automatic Administration 
    864872system, your model must have an inner ``"class Admin"``, like so:: 
    865873 
     
    870878            # ... 
    871879 
    872 The Admin class gives instructions to Django on how to display the Model  
     880The Admin class gives instructions to Django on how to display the Model 
    873881to the Administration system. 
    874882 
     
    10141022======== 
    10151023 
    1016 The Manager is the interface through which database query operations  
     1024The Manager is the interface through which database query operations 
    10171025are provided to Django applications. At least one Manager exists for 
    10181026every model in a Django application. 
    10191027 
    1020 By default, Django will add a Manager with the name of ``objects`` to  
     1028By default, Django will add a Manager with the name of ``objects`` to 
    10211029every Django model. However, if you wish to use ``objects`` as a field 
    10221030name, or if you wish to use a name other than ``objects`` for the Manager, 
    1023 you can rename the Manager on a per-model basis. To rename the Manager  
    1024 for a given class, define a class attribute of type models.Manager()  
     1031you can rename the Manager on a per-model basis. To rename the Manager 
     1032for a given class, define a class attribute of type models.Manager() 
    10251033on that model. For example:: 
    10261034 
    10271035    from django.db import models 
    1028      
     1036 
    10291037    class Person(models.Model): 
    10301038        #... 
    10311039        people = models.Manager() 
    10321040 
    1033 In this example, ``Person.objects.all()`` will generate an error, but  
     1041In this example, ``Person.objects.all()`` will generate an error, but 
    10341042``Person.people.all()`` will provide a list of all ``Person`` objects. 
    10351043 
    10361044Managers can also be customized. This is achieved by extending the 
    10371045base Manager class, and instantiating the new Manager on your model. 
    1038 There are two reasons that you may want to customize a Manager: firstly,  
    1039 to add utility methods to the Manager, and secondly, to modify the  
     1046There are two reasons that you may want to customize a Manager: firstly, 
     1047to add utility methods to the Manager, and secondly, to modify the 
    10401048initial Query Set provided by the Manager. 
    10411049 
     
    10481056        def move_house(self): 
    10491057            # Some logic to help a person move house 
    1050              
     1058 
    10511059        # Modify the initial Query Set provided by the manager 
    10521060        def get_query_set(self): 
    10531061            return super(Manager, self).get_query_set().filter(name__startswith="Fred") 
    1054          
     1062 
    10551063    class Person(models.Model): 
    10561064        #... 
     
    10711079        def get_query_set(self): 
    10721080            return super(Manager, self).get_query_set().filter(sex='F') 
    1073              
     1081 
    10741082    class Person(models.Model): 
    10751083        #... 
     
    10781086        women = FemaleManager() 
    10791087 
    1080 ... will allow end users to request ``Person.men.all()``, ``Person.women.all()``,  
     1088... will allow end users to request ``Person.men.all()``, ``Person.women.all()``, 
    10811089and ``Person.people.all()``, yielding predictable results. 
    10821090 
    10831091If you are going to install a customized Manager, be warned that the first 
    10841092Manager that Django encounters in a model definition has special status. 
    1085 Django interprets the first Manager defined in a class as the default Manager.  
    1086 Certain operations use the default Manager to obtain lists of objects, so it  
     1093Django interprets the first Manager defined in a class as the default Manager. 
     1094Certain operations use the default Manager to obtain lists of objects, so it 
    10871095is generally a good idea for the first Manager to be relatively unfiltered. 
    10881096In the last example, ``people`` is defined first - so the default Manager 
     
    11481156-------------------- 
    11491157 
    1150 If you want to add a method to the Model, rather than instances of the model,  
     1158If you want to add a method to the Model, rather than instances of the model, 
    11511159you can use the Python ``staticmethod`` and ``classmethod`` operators. For 
    11521160example:: 
     
    11581166            return get_list(delivered__exact=False) 
    11591167        get_pizzas_to_deliver = staticmethod(get_pizzas_to_deliver) 
    1160          
     1168 
    11611169Or, using Python 2.4 decorators:: 
    1162      
     1170 
    11631171    # ... 
    11641172    @staticmethod 
    11651173    def get_pizzas_to_deliver(): 
    11661174        # ... 
    1167          
     1175 
    11681176This will make the top-level ``pizzas`` module have a ``get_pizzas_to_deliver()`` 
    11691177method:: 
     
    11961204Feel free to write custom SQL statements in custom model methods and 
    11971205module-level methods. The object ``django.db.connection`` object represents 
    1198 the current database connection. To use it, call ``connection.cursor()`` to  
     1206the current database connection. To use it, call ``connection.cursor()`` to 
    11991207get a cursor object. Then, call ``cursor.execute(sql, [params])`` 
    12001208to execute the SQL and ``cursor.fetchone()`` or ``cursor.fetchall()`` to return 
     
    12351243 
    12361244Once you have created your model, you have to tell Django about your new application. 
    1237 This is done by editing your settings file and adding the name of the module that  
    1238 contains your models module to the ``INSTALLED_APPS`` tuple.  
    1239  
    1240 For example, if the models for your application are contained in the module  
    1241 ``project.myapp.models`` (the package structure that is created for an application  
     1245This is done by editing your settings file and adding the name of the module that 
     1246contains your models module to the ``INSTALLED_APPS`` tuple. 
     1247 
     1248For example, if the models for your application are contained in the module 
     1249``project.myapp.models`` (the package structure that is created for an application 
    12421250by the ``django-admin.py startapp`` script), ``INSTALLED_APPS`` should read, in part:: 
    12431251 
     
    12521260 
    12531261It's perfectly OK to relate a model to one from another module. To do this, 
    1254 just import the model module at the top of your model module. Then, just  
     1262just import the model module at the top of your model module. Then, just 
    12551263refer to the other model class wherever needed. For example:: 
    12561264