#683 closed defect (fixed)
[patch] Saving with custom db_column fails
| Reported by: | 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)
Change History (7)
comment:1 by , 20 years ago
by , 20 years ago
| Attachment: | meta__init__.patch added |
|---|
patch changing .column to .name when it seems to mean that.
comment:2 by , 20 years ago
| Summary: | Saving with PK other than "id" fails → [patch] Saving with PK other than "id" fails |
|---|
comment:3 by , 20 years ago
| Status: | new → assigned |
|---|
comment:4 by , 20 years ago
| Summary: | [patch] Saving with PK other than "id" fails → [patch] Saving with custom db_column fails |
|---|
by , 20 years ago
| Attachment: | 683_fix.patch added |
|---|
A patch that fixes it by introducing Field.attname
comment:5 by , 20 years ago
| Resolution: | → fixed |
|---|---|
| Status: | assigned → closed |
Note:
See TracTickets
for help on using tickets.
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.