diff options
Diffstat (limited to 'docs/model-api.txt')
-rw-r--r-- | docs/model-api.txt | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/docs/model-api.txt b/docs/model-api.txt index c6707a691b..0dc98416a3 100644 --- a/docs/model-api.txt +++ b/docs/model-api.txt @@ -445,7 +445,8 @@ empty value. If a field has ``blank=False``, the field will be required. ``choices`` ~~~~~~~~~~~ -A list of 2-tuples to use as choices for this field. +An iterable (e.g., a list or tuple) of 2-tuples to use as choices for this +field. If this is given, Django's admin will use a select box instead of the standard text field and will limit choices to the choices given. @@ -481,6 +482,12 @@ or outside your model class altogether:: class Foo(models.Model): gender = models.CharField(maxlength=1, choices=GENDER_CHOICES) +Finally, note that choices can be any iterable object -- not necessarily a +list or tuple. This lets you construct choices dynamically. But if you find +yourself hacking ``choices`` to be dynamic, you're probably better off using +a proper database table with a ``ForeignKey``. ``choices`` is meant for static +data that doesn't change much, if ever. + ``core`` ~~~~~~~~ @@ -1627,10 +1634,10 @@ to happen whenever you save an object. For example:: name = models.CharField(maxlength=100) tagline = models.TextField() - def save(self): - do_something() - super(Blog, self).save() # Call the "real" save() method. - do_something_else() + def save(self): + do_something() + super(Blog, self).save() # Call the "real" save() method. + do_something_else() You can also prevent saving:: @@ -1638,11 +1645,11 @@ You can also prevent saving:: name = models.CharField(maxlength=100) tagline = models.TextField() - def save(self): - if self.name == "Yoko Ono's blog": - return # Yoko shall never have her own blog! - else: - super(Blog, self).save() # Call the "real" save() method. + def save(self): + if self.name == "Yoko Ono's blog": + return # Yoko shall never have her own blog! + else: + super(Blog, self).save() # Call the "real" save() method. .. _database API docs: http://www.djangoproject.com/documentation/db_api/ |