Django

Code

Changeset 67

Show
Ignore:
Timestamp:
07/15/05 15:37:03 (3 years ago)
Author:
jacob
Message:

Yes yes yes -- more documentation improvements

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/docs/db-api.txt

    r59 r67  
    306306==================== 
    307307 
    308 ... 
     308Creating new objects (i.e. ``INSERT``) is done by creating new instances 
     309of objects then calling save() on them:: 
     310 
     311    >>> p = polls.Poll(id=None,  
     312    ...                slug="eggs", 
     313    ...                question="How do you like your eggs?", 
     314    ...                pub_date=datetime.datetime.now(), 
     315    ...                expire_date=some_future_date) 
     316    >>> p.save() 
     317     
     318Calling ``save()`` on an object with an id if ``None`` signifies to 
     319Django that the object is new and should be inserted. 
     320 
     321Related objects (i.e. ``Choices``) are created using convience functions:: 
     322 
     323    >>> p.add_choice(choice="Over easy", votes=0) 
     324    >>> p.add_choice(choice="Scrambled", votes=0) 
     325    >>> p.add_choice(choice="Fertilized", votes=0) 
     326    >>> p.add_choice(choice="Poached", votes=0) 
     327    >>> p.get_choice_count() 
     328    4 
     329     
     330Each of those ``add_choice`` methods is equivilent to (except obviously much 
     331simpler than):: 
     332 
     333    >>> c = polls.Choice(id=None, 
     334    ...                  poll_id=p.id, 
     335    ...                  choice="Over easy", 
     336    ...                  votes=0) 
     337    >>> c.save() 
     338     
     339Note that when using the `add_foo()`` methods, you do not give any value 
     340for the ``id`` field, nor do you give a value for the field that stores  
     341the relation (``poll_id`` in this case). 
     342 
     343Deleting objects 
     344================ 
     345 
     346Just cause we're crazy like that, the delete method is named ``delete()``. 
     347Yeah, you never know what we're going to do next. 
     348 
  • django/trunk/docs/faq.txt

    r63 r67  
    6767 
    6868`Simon Willison`_ 
    69     Simon is a well-respected Web developer from England. He had a one-year stint 
    70     at World Online, during which time he and Adrian developed Django from scratch. 
    71     He's enthusiastic, he's passionate about best practices in Web development, and 
    72     he really likes squirrels. Probably to a fault. He went back to England to 
    73     finish his degree and is poised to continue doing big, exciting things on the Web. 
    74     Read his weblog at `simon.incutio.com`_. 
     69    Simon is a well-respected Web developer from England. He had a one-year 
     70    stint at World Online, during which time he and Adrian developed Django from 
     71    scratch. He's enthusiastic, he's passionate about best practices in Web 
     72    development, and he really likes squirrels. Probably to a fault. He went 
     73    back to England to finish his degree and is poised to continue doing big, 
     74    exciting things on the Web. Read his weblog at `simon.incutio.com`_. 
    7575 
    7676`Jacob Kaplan-Moss`_ 
     
    103103We're working on this documentation as you read this. 
    104104 
     105What are Django's prerequisites? 
     106-------------------------------- 
     107 
     108Django requires Python_ 2.3 or later, Apache2_, and mod_python_.  You'll 
     109also need a database engine; PostgreSQL_ is recommended, and MySQL_ is 
     110supported. 
     111 
     112We're currently working on expanding those options: WSGI_ support is in the 
     113works (which will allow Django to run under CGI, FCGI, etc.), as is support for 
     114a number of other database backends. 
     115 
     116.. _Python: http://www.python.org/ 
     117.. _Apache2: http://httpd.apache.org/ 
     118.. _mod_python: http://www.modpython.org/ 
     119.. _PostgreSQL: http://www.postgresql.org/ 
     120.. _MySQL: http://www.mysql.com/ 
     121.. _WSGI: http://www.python.org/peps/pep-0333.html 
     122 
    105123The admin interface 
    106124=================== 
     
    109127------------------------------------------------------------------ 
    110128 
    111 We think it's very purty, but if you don't agree, you can modify the admin site's 
    112 presentation by editing the CSS stylesheet and/or associated image files. The 
    113 site is built using semantic HTML, so any changes you'd like to make should be 
    114 possible by editing the CSS stylesheet. We've got a `guide to the CSS used 
    115 in the admin`_ to get you started. 
     129We think it's very purty, but if you don't agree, you can modify the admin 
     130site's presentation by editing the CSS stylesheet and/or associated image files. 
     131The site is built using semantic HTML, so any changes you'd like to make should 
     132be possible by editing the CSS stylesheet. We've got a `guide to the CSS used in 
     133the admin`_ to get you started. 
    116134 
    117135.. _`guide to the CSS used in the admin`: http://www.djangoproject.com/documentation/admin_css/ 
  • django/trunk/docs/model-api.txt

    r45 r67  
    181181    ``null``                If ``True`` empty values in the field will be  
    182182                            stored as ``NULL`` in the database.   
    183                              
    184                             XXX does null imply blank? XXX 
    185183         
    186184    ``primary_key``         If ``True`` this field is the primary key for the 
     
    303301        meta.ForeignKey(Pizza) 
    304302                 
    305     ``ForeignKey`` fields take a large number of options for defining how the  
    306     relationship should work: 
     303    ``ForeignKey`` fields take a large number of extra options for defining how 
     304    the relationship should work: 
    307305     
    308306    =======================  ============================================================ 
     
    365363                             Not used with ``edit_inline``. 
    366364                              
    367     ``rel_name``             The name of the relation.  In the above exmaple, 
     365    ``rel_name``             The name of the relation.  In the above example, 
    368366                             this would default to 'pizza' (so that the  
    369367                             ``Toppings`` object would have a ``get_pizza()`` 
     
    432430     
    433431``ManyToManyField`` 
    434     XXX document once Adrian reworks this XXX 
    435      
     432    A many-to-many relation to another object.  For example (taken from the 
     433    ``core.flatfiles`` object:: 
     434     
     435        class FlatFile(meta.Model): 
     436            fields = ( 
     437                ... 
     438                meta.ManyToManyField(Site), 
     439            ) 
     440     
     441    Many-to-many relations are a bit different from other fields.  First, they 
     442    aren't actually a field per se since they use a intermediary join table. 
     443    Second, they don't take any of the same options as the rest of the fields, 
     444    the only options taken are: 
     445     
     446    =======================  ============================================================ 
     447    Option                   Description 
     448    =======================  ============================================================ 
     449    ``related_name``         See the description of ``related_name`` in  
     450                             ``ManyToOneField``, above. 
     451                              
     452    ``filter_interface``     Use a nifty unobtrusive Javascript "filter" interface  
     453                             instead of the usability-challenged ``<select multiple>``. 
     454                             The value should be ``meta.HORIZONTAL`` or ``meta.VERTICAL`` 
     455                             (i.e. should the interface be stacked horizontally or 
     456                             vertically). 
     457                              
     458    ``limit_choices_to``     See the description under ``ManyToOneField``, above. 
     459    =======================  ============================================================ 
     460 
    436461``NullBooleanField`` 
    437462    Like a ``BooleanField``, but allows ``NULL`` as one of the options.  Use this 
    438463    instead of a ``BooleanField`` with ``null=True`` . 
    439464     
     465``OneToOneField`` 
     466    Signifies a one-to-one relationship.  This is most useful on the primary key 
     467    of an object when that object "extends" another object in some way.   
     468     
     469    For example, if you are building a database of "places", you would build pretty 
     470    standard stuff like address, phone number, etc. in the database.  If you then 
     471    wanted to build a database of restaurants on top of the places, instead of 
     472    repeating yourself and replicating those fields in the restaurants object, you 
     473    could make ``Restaurant`` have a ``OneToOneField`` to ``Place`` (since 
     474    a restaurant "is-a" place).  This ``OneToOneField`` will actually replace 
     475    the primary key ``id`` field (since one-to-one relations share the same 
     476    primary key), and has a few in the admin interface: 
     477     
     478        * No selection interface is displayed on ``Restaurant`` pages; there will 
     479          be one (and only one) ``Restaurant`` for each place. 
     480           
     481        * On the ``Restaurant`` change list, every single ``Place`` -- weather it 
     482          has an associated ``Restaurant`` or not -- will be displayed.  Adding 
     483          a ``Restaurant`` to a ``Place`` just means filling out the required 
     484          ``Restaurant`` fields. 
     485 
    440486``PhoneNumberField`` 
    441487    Validates that the value is a valid phone number. 
     
    574620     
    575621``ordering`` 
    576     An ordering tuple (see the `Options for models`_, above) that gives a  
    577     different ordering for the admin change list.  If not given, the  
    578     model's default ordering will be used. 
     622    An ordering tuple (see the `Options for models`_, above) that gives a 
     623    different ordering for the admin change list.  If not given, the model's 
     624    default ordering will be used. 
    579625     
    580626``save_as`` 
    581     Enables a "save as" feature on object pages.  Normally, objects have 
    582     three save options: "Save", "Save and continue editing", and "Save 
    583     and add another".   If ``save_as`` is ``True``, "Save and add another" 
    584     will be replaced by a "Save as" button. 
     627    Enables a "save as" feature on object pages.  Normally, objects have three 
     628    save options: "Save", "Save and continue editing", and "Save and add 
     629    another".   If ``save_as`` is ``True``, "Save and add another" will be 
     630    replaced by a "Save as" button. 
    585631     
    586632``save_on_top`` 
    587     If this option is ``True``, object pages will have the save buttons 
    588     across the top as well as at the bottom of the page. 
     633    If this option is ``True``, object pages will have the save buttons across 
     634    the top as well as at the bottom of the page. 
    589635     
    590636``search_fields`` 
     
    592638    obviously, be some kind of text field. 
    593639     
     640Model methods 
     641============= 
     642 
     643There are a number of methods you can define on model objects to control the 
     644object's behavior.  First, any methods you define will be available as methods 
     645of object instances.  For example:: 
     646 
     647    class Pizza(meta.Model): 
     648        fields = ( 
     649            ... 
     650        )   
     651         
     652        def is_disgusting(self): 
     653            return "anchovices" in [topping.name for topping in self.get_topping_list()] 
     654                
     655Now, every ``Pizza`` object will have a ``is_disgusting()`` method.   
     656 
     657There are a few object methods that have special meaning: 
     658 
     659``__repr__`` 
     660    Django uses ``repr(obj)`` in a number of places, the most notable as the 
     661    value inserted into a template when it displays an object.  Thus, you should 
     662    return a nice, human-readable string for the object's ``__repr__``. 
     663     
     664``get_absolute_url`` 
     665    If an object defines a ``get_absolute_url`` method, it is used   to 
     666    associate a URL with an object.  For example: 
     667     
     668        def get_absolute_url(self): 
     669            return "/pizzas/%i/" % self.id 
     670             
     671    The most useful place this is used is in the admin interface; if an object 
     672    defines ``get_absolute_url`` then the object detail page will have a "View 
     673    on site" link that will jump you directly to the object's public view. 
     674     
     675``_pre_save`` 
     676    This method will be called just before the object is saved into the 
     677    database; you can use it to (for example) calculate aggregate values from 
     678    other fields before the object is saved. 
     679     
     680``_post_save`` 
     681    Called just after the object is saved to the database.  This could be used 
     682    to update other tables, update cached information, etc. 
     683     
     684Module-level methods 
     685-------------------- 
     686 
     687Since each data class in effect turns into a module, there are times you'll want 
     688to write methods that live in that module.  Any model method that begins with 
     689"_module_" is turned into a module-level method:: 
     690 
     691    class Pizza(meta.Model): 
     692        fields = ( 
     693            ... 
     694        ) 
     695         
     696        def _module_get_pizzas_to_deliver(): 
     697            return get_list(delivered__exact=False) 
     698             
     699This will make the top-level ``pizzas`` module have a ``get_pizzas_to_deliver()`` 
     700method:: 
     701 
     702    >>> from django.models.pizza_hut import pizzas 
     703    >>> pizzas.get_pizzas_to_deliver() 
     704    [ ... ] 
     705     
     706Note that the scope of these methods is modified to be the same has the module 
     707scope (so that's how the raw ``get_list`` works). 
     708 
     709Manipulator methods 
     710------------------- 
     711 
     712Similarly, you can add methods to the object's manipulators (see the formfields 
     713documentation for more on manipulators) by defining methods that being with 
     714"_manipulator_".  This is most useful for providing custom validators for certain 
     715fields since manipulators automatically call any method that begins with 
     716"validate":: 
     717 
     718    class Pizza(meta.Model): 
     719        fields = ( 
     720            ... 
     721        ) 
     722         
     723        def _manipulator_validate_customer_id(self, field_data, all_data): 
     724            from django.core import validators 
     725            from django.conf.settings import BAD_CUSTOMER_IDS 
     726             
     727            if int(field_data) in BAD_CUSTOMER_IDS: 
     728                raise validators.ValidationError("We don't deliver to this customer") 
     729                 
     730 
     731