Ticket #6445: 6645.diff
File 6645.diff, 2.0 KB (added by , 17 years ago) |
---|
-
django/db/models/fields/related.py
548 548 params['choices'] = self.get_choices_default() 549 549 return field_objs, params 550 550 551 def get_default(self): 552 "Here we check if the default value is an object and return the to_field if so." 553 field_default = super(ForeignKey, self).get_default() 554 if isinstance(field_default, self.rel.to): 555 return getattr(field_default, self.rel.get_related_field().attname) 556 return field_default 557 551 558 def get_manipulator_field_objs(self): 552 559 rel_field = self.rel.get_related_field() 553 560 if self.rel.raw_id_admin and not isinstance(rel_field, AutoField): -
tests/regressiontests/model_fields/models.py
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 """} -
docs/model-api.txt
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. 630 If callable it will be used every time a new object is created. 630 631 631 632 ``editable`` 632 633 ~~~~~~~~~~~~