Changeset 67
- Timestamp:
- 07/15/05 15:37:03 (3 years ago)
- Files:
-
- django/trunk/docs/db-api.txt (modified) (1 diff)
- django/trunk/docs/faq.txt (modified) (3 diffs)
- django/trunk/docs/model-api.txt (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/docs/db-api.txt
r59 r67 306 306 ==================== 307 307 308 ... 308 Creating new objects (i.e. ``INSERT``) is done by creating new instances 309 of 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 318 Calling ``save()`` on an object with an id if ``None`` signifies to 319 Django that the object is new and should be inserted. 320 321 Related 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 330 Each of those ``add_choice`` methods is equivilent to (except obviously much 331 simpler than):: 332 333 >>> c = polls.Choice(id=None, 334 ... poll_id=p.id, 335 ... choice="Over easy", 336 ... votes=0) 337 >>> c.save() 338 339 Note that when using the `add_foo()`` methods, you do not give any value 340 for the ``id`` field, nor do you give a value for the field that stores 341 the relation (``poll_id`` in this case). 342 343 Deleting objects 344 ================ 345 346 Just cause we're crazy like that, the delete method is named ``delete()``. 347 Yeah, you never know what we're going to do next. 348 django/trunk/docs/faq.txt
r63 r67 67 67 68 68 `Simon Willison`_ 69 Simon is a well-respected Web developer from England. He had a one-year stint70 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, and72 he really likes squirrels. Probably to a fault. He went back to England to73 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`_. 75 75 76 76 `Jacob Kaplan-Moss`_ … … 103 103 We're working on this documentation as you read this. 104 104 105 What are Django's prerequisites? 106 -------------------------------- 107 108 Django requires Python_ 2.3 or later, Apache2_, and mod_python_. You'll 109 also need a database engine; PostgreSQL_ is recommended, and MySQL_ is 110 supported. 111 112 We're currently working on expanding those options: WSGI_ support is in the 113 works (which will allow Django to run under CGI, FCGI, etc.), as is support for 114 a 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 105 123 The admin interface 106 124 =================== … … 109 127 ------------------------------------------------------------------ 110 128 111 We think it's very purty, but if you don't agree, you can modify the admin site's112 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 inthe admin`_ to get you started.129 We think it's very purty, but if you don't agree, you can modify the admin 130 site's presentation by editing the CSS stylesheet and/or associated image files. 131 The site is built using semantic HTML, so any changes you'd like to make should 132 be possible by editing the CSS stylesheet. We've got a `guide to the CSS used in 133 the admin`_ to get you started. 116 134 117 135 .. _`guide to the CSS used in the admin`: http://www.djangoproject.com/documentation/admin_css/ django/trunk/docs/model-api.txt
r45 r67 181 181 ``null`` If ``True`` empty values in the field will be 182 182 stored as ``NULL`` in the database. 183 184 XXX does null imply blank? XXX185 183 186 184 ``primary_key`` If ``True`` this field is the primary key for the … … 303 301 meta.ForeignKey(Pizza) 304 302 305 ``ForeignKey`` fields take a large number of options for defining how the306 relationship should work:303 ``ForeignKey`` fields take a large number of extra options for defining how 304 the relationship should work: 307 305 308 306 ======================= ============================================================ … … 365 363 Not used with ``edit_inline``. 366 364 367 ``rel_name`` The name of the relation. In the above ex maple,365 ``rel_name`` The name of the relation. In the above example, 368 366 this would default to 'pizza' (so that the 369 367 ``Toppings`` object would have a ``get_pizza()`` … … 432 430 433 431 ``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 436 461 ``NullBooleanField`` 437 462 Like a ``BooleanField``, but allows ``NULL`` as one of the options. Use this 438 463 instead of a ``BooleanField`` with ``null=True`` . 439 464 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 440 486 ``PhoneNumberField`` 441 487 Validates that the value is a valid phone number. … … 574 620 575 621 ``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'sdefault 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. 579 625 580 626 ``save_as`` 581 Enables a "save as" feature on object pages. Normally, objects have 582 three save options: "Save", "Save and continue editing", and "Save583 an d add another". If ``save_as`` is ``True``, "Save and add another"584 will bereplaced 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. 585 631 586 632 ``save_on_top`` 587 If this option is ``True``, object pages will have the save buttons 588 acrossthe 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. 589 635 590 636 ``search_fields`` … … 592 638 obviously, be some kind of text field. 593 639 640 Model methods 641 ============= 642 643 There are a number of methods you can define on model objects to control the 644 object's behavior. First, any methods you define will be available as methods 645 of 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 655 Now, every ``Pizza`` object will have a ``is_disgusting()`` method. 656 657 There 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 684 Module-level methods 685 -------------------- 686 687 Since each data class in effect turns into a module, there are times you'll want 688 to 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 699 This will make the top-level ``pizzas`` module have a ``get_pizzas_to_deliver()`` 700 method:: 701 702 >>> from django.models.pizza_hut import pizzas 703 >>> pizzas.get_pizzas_to_deliver() 704 [ ... ] 705 706 Note that the scope of these methods is modified to be the same has the module 707 scope (so that's how the raw ``get_list`` works). 708 709 Manipulator methods 710 ------------------- 711 712 Similarly, you can add methods to the object's manipulators (see the formfields 713 documentation for more on manipulators) by defining methods that being with 714 "_manipulator_". This is most useful for providing custom validators for certain 715 fields 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
