Opened 19 years ago

Closed 19 years ago

Last modified 18 years ago

#683 closed defect (fixed)

[patch] Saving with custom db_column fails

Reported by: jdunck@… Owned by: Adrian Holovaty
Component: Metasystem Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Given

class Poll(meta.Model):
    poll_id = meta.IntegerField(db_column="poll_pk", primary_key=True)
    question = meta.CharField(maxlength=200)
    pub_date = meta.DateTimeField('date published')

this fails:

from django.models.polls import polls
from datetime import datetime
p = polls.Poll(question="spam?", pub_date=datetime(2005,10,22,19,22))
p.save()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.4/site-packages/django/utils/functional.py", line 3, in _curried
    return args[0](*(args[1:]+moreargs), **dict(kwargs.items() + morekwargs.items()))
  File "/usr/lib/python2.4/site-packages/django/core/meta/__init__.py", line 793, in method_save
    pk_val = getattr(self, opts.pk.column)
AttributeError: 'Poll' object has no attribute 'poll_pk'

but:

p.poll_pk = None
p.save()
p2 = polls.get_object(question__exact="spam?")
>>> p2.poll_pk
2

Attachments (2)

meta__init__.patch (26.9 KB ) - added by jdunck@… 19 years ago.
patch changing .column to .name when it seems to mean that.
683_fix.patch (29.0 KB ) - added by Adrian Holovaty 19 years ago.
A patch that fixes it by introducing Field.attname

Download all attachments as: .zip

Change History (7)

comment:1 by jdunck@…, 19 years ago

OK, unless I'm hugely wrong, this is the tip of an iceberg.

Looking in core.meta.init.py, it seems to me that there're lots of places using field.column when it really means field.name.

Of course, meta.init.py is making my head hurt, so I could be wrong.

Another example of this problem:

On latest trunk, given:

from django.core import meta

class Poll(meta.Model):
    poll_id = meta.IntegerField(db_column="poll_pk", primary_key=True)
    question = meta.CharField(maxlength=200)
    pub_date = meta.DateTimeField('date published')

class Choice(meta.Model):
    poll = meta.ForeignKey(Poll, db_column="poll_pk")
    choice = meta.CharField(maxlength=200)
    votes = meta.IntegerField()

result:

>>> from django.models.polls import polls, choices
>>> p = polls.get_list()[0]
>>> p
<Poll object>
>>> c = choices.Choice(votes=0, choice="yes, please")
>>> p.add_choice(c)
adding related: {'poll': <Poll object>, 'id': None, 'choice': <Choice object>}
trying to get poll_id on <Poll object>
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "django/utils/functional.py", line 3, in _curried
  File "__init__.py", line 954, in method_add_related
    obj = rel_mod.Klass(**init_kwargs)
  File "django/utils/functional.py", line 3, in _curried
  File "__init__.py", line 764, in method_init
    raise TypeError, "Invalid value: %r should be a %s instance, not a %s" % (f.name, f.rel.to, type(rel_obj))
TypeError: Invalid value: 'poll' should be a <Options for polls> instance, not a <class 'django.models.polls.Poll'>

(Note the prints coming out of the .add_choice are my attempts to determine what's going on).

In the hopes that it saves some time, I'm attaching a patch to meta.init.py; I basically changed every place that seemed like it meant ".name" when using ".column"; 9 tests fail, but 8 of these are many_to_one_null not having "a.reporter_id", which I think is related to the problem above, which I can't wrap my head around right now.

by jdunck@…, 19 years ago

Attachment: meta__init__.patch added

patch changing .column to .name when it seems to mean that.

comment:2 by hugo, 19 years ago

Summary: Saving with PK other than "id" fails[patch] Saving with PK other than "id" fails

comment:3 by Adrian Holovaty, 19 years ago

Status: newassigned

comment:4 by Adrian Holovaty, 19 years ago

Summary: [patch] Saving with PK other than "id" fails[patch] Saving with custom db_column fails

by Adrian Holovaty, 19 years ago

Attachment: 683_fix.patch added

A patch that fixes it by introducing Field.attname

comment:5 by Adrian Holovaty, 19 years ago

Resolution: fixed
Status: assignedclosed

(In [1150]) Fixed #683 -- Lightly refactored meta.fields.Field to add an attname attribute.

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