Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #6445 -- Allow model instances to be used as a default for Fore…

…ignKeys

(via a callable). Also updates the documentation of the "default" attribute to
indicate a callable can be used. Thanks, Philipe Raoult.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@7331 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit b3b8422363dd450d1ff1a376ab1f74cf2e22ce1b 1 parent 597f9d6
Malcolm Tredinnick authored March 20, 2008
7  django/db/models/fields/related.py
@@ -548,6 +548,13 @@ def prepare_field_objs_and_params(self, manipulator, name_prefix):
3  docs/model-api.txt
@@ -626,7 +626,8 @@ option is ignored.
626 626
 ``default``
627 627
 ~~~~~~~~~~~
628 628
 
629  
-The default value for the field.
  629
+The default value for the field. This can be a value or a callable object. If
  630
+callable it will be called every time a new object is created.
630 631
 
631 632
 ``editable``
632 633
 ~~~~~~~~~~~~
24  tests/regressiontests/model_fields/models.py
... ...
@@ -0,0 +1,24 @@
  1
+
  2
+from django.db import models
  3
+
  4
+class Foo(models.Model):
  5
+    a = models.CharField(max_length=10)
  6
+
  7
+def get_foo():
  8
+    return Foo.objects.get(id=1)
  9
+
  10
+class Bar(models.Model):
  11
+    b = models.CharField(max_length=10)
  12
+    a = models.ForeignKey(Foo, default=get_foo)
  13
+
  14
+__test__ = {'API_TESTS':"""
  15
+# Create a couple of Places.
  16
+>>> f = Foo.objects.create(a='abc')
  17
+>>> f.id
  18
+1
  19
+>>> b = Bar(b = "bcd")
  20
+>>> b.a
  21
+<Foo: Foo object>
  22
+>>> b.save()
  23
+
  24
+"""}

0 notes on commit b3b8422

Please sign in to comment.
Something went wrong with that request. Please try again.