Opened 16 years ago

Closed 16 years ago

Last modified 16 years ago

#7971 closed (invalid)

default doesn't work for foreign key fields

Reported by: mspang Owned by: nobody
Component: Database layer (models, ORM) Version: 1.0-alpha
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Model:

class A(models.Model):
  id = models.CharField(primary_key=True, max_length=5)

class B(models.Model):
  a = models.ForeignKey(A, default=A)

Code:

>>> b = nags_models.B()
>>> b.a
Traceback (most recent call last):
  File "<console>", line 1, in ?
  File "/opt/django/db/models/fields/related.py", line 240, in __get__
    rel_obj = QuerySet(self.field.rel.to).get(**params)
  File "/opt/django/db/models/query.py", line 301, in get
    raise self.model.DoesNotExist("%s matching query does not exist."
DoesNotExist: A matching query does not exist.
>>> b.a = models.A()
>>> b.a
<A: A object>

Change History (2)

comment:1 by Russell Keith-Magee, 16 years ago

Resolution: invalid
Status: newclosed

I don't know what gave you the impression that this _should_ work, but it doesn't make any sense. default takes an instance, or a callable that returns an instance. 'A' is a class, so it can't be used as a default.

If you want to populate the field 'B.a' with a new instance of A, then you need to define a function that returns a new instance, and use that function as a callable value to default: e.g.:

def new_a():
    return A()

class B(models.Model):
    a = models.ForeignKey(A, default=new_a)

comment:2 by Malcolm Tredinnick, 16 years ago

The main problem with the approach in the description -- even though A seems to satisfy the constraints of being a callable -- is that it would lead to a database integrity issue if it proceeded further. The A() instance has not been saved, so it does not have a corresponding row in the database table and thus trying to refer to that (non-existent) row from the B table would lead to trouble. Thus you have to use a function and also save the instance before returning it.

Note: See TracTickets for help on using tickets.
Back to Top