*_set (ForeignKey accessors) broken?

23 views
Skip to first unread message

m h

unread,
Jul 6, 2006, 12:50:56 AM7/6/06
to django-d...@googlegroups.com, django...@googlegroups.com
I'll just post the contents of my bug report. Has anyone else ran
into this? Any hints, suggestions?


http://code.djangoproject.com/ticket/2297

I revved to v3278 and my app broke. In my view I adjust some fields of
the model.

My model looks something like this (quite simplified)

class Foo(models.Model):
name = models.CharField(maxlength=30)

class Bar(model.Model):
foo = models.ForeignKey(Foo)
data = models.CharField(maxlength=30)

later on I iterate over my bars and try and get my foos from them. ie

for foo in bar.foo_set.all():
#do something with foo

I now get an attribute error when trying to do this:

AttributeError at /fooapp/
'Bar' object has no attribute 'foo_set'
Request Method: GET
Request URL: http://localhost:8000/fooapp/
Exception Type: AttributeError
Exception Value: 'Bar' object has no attribute 'foo_set'

Just wondering if there was a change that broke then _set functionality recently

I apologize for how vague this is. I have another model that has a
foreign key and code similar to the above still works with that. I've
briefly diffed the sources didn't chance on anything that looked to
cause this change....

-matt

Malcolm Tredinnick

unread,
Jul 6, 2006, 1:12:27 AM7/6/06
to django-d...@googlegroups.com
On Wed, 2006-07-05 at 22:50 -0600, m h wrote:
> I'll just post the contents of my bug report. Has anyone else ran
> into this? Any hints, suggestions?
>
>
> http://code.djangoproject.com/ticket/2297

This isn't a bug. You are using foo_set incorrectly (it is not an
attribute on the Bar model, as the error indicates). Either bar.foo or
foo_instance.foo_set is what you want to use.

Regards,
Malcolm


m h

unread,
Jul 6, 2006, 12:03:37 PM7/6/06
to django-d...@googlegroups.com
I'm sorry, I messed up my foos and bars. Let me try again. (Not using
foo nor bar).

I have a Component object, a Version object and a Data object (that
holds additional data about the version object). They look like
this::


class Component(models.Model):
name = models.CharField(maxlenth=30)
...

class Version(models.Model):
component = models.ForeignKey(Component)
version = models.CharField(maxlength=30)
...

class Data(models.Model):
meta_data = models.CharField(maxlength=30)
version = models.ForeignKey(Version)

Now given a component, I want to get all the version instances and
alll the data associated with it. So I do something like this:

for version in component_instance.version_set.all():
for data in version.data_set.all():
#adjust meta_data on data

AttributeError at /myapp/
'Version' object has no attribute 'data_set'
Request Method: GET
Request URL: http://localhost:8000/myapp/
Exception Type: AttributeError
Exception Value: 'Version' object has no attribute 'data_set'
Exception Location: /home/matt/.views.py in _add_metadata, line 293

So this is the weird thing. Accessing _set.all() works on the
component_instance, but fails on the next line... Is _set.all() no
longer returning instances? Again, this worked before updating svn.

-matt

Curtis

unread,
Jul 6, 2006, 7:07:45 PM7/6/06
to Django developers
Hey Matt,

After updating to the most recent version in svn, I used your example
and it works for me. Could the problem be in part of your model that
you're not showing?

In [3]: c = Component.objects.all()[0]
In [4]: for version in c.version_set.all():
...: for data in version.data_set.all():
...: print data
...:
Data object

Cheers,

-Curt

Reply all
Reply to author
Forward
0 new messages