Potential bug with deletion of abstract models with related attributes

21 views
Skip to first unread message

Julien

unread,
Jun 2, 2008, 4:56:30 AM6/2/08
to Django developers
Hi,

I've identified something that appears to me as a bug. I found a quick
fix, but I'd like to hear from some advised people to know what's the
best way to go, or if I'm simply missing the point.

You can copy/paste the code from: http://dpaste.com/hold/54289/

Simply put, there are 2 models (UserProfile and Project) which both
inherit from some abstract models that centralize the management of
file galleries (one type of gallery is an avatar gallery, and the
other type is a regular file gallery).

Now, to replicate the problem in the admin, create a new project, then
create a new file gallery and set it as the 'file_gallery' attribute
for that project.

Then, try to delete the file gallery object, and you should get the
error "'FileGallery' object has no attribute 'project_filegalleried'"
as in: http://dpaste.com/hold/54288/

The error comes from the end of the method the method at
django.db.models.base._collect_sub_objects:

for sub_obj in getattr(self, rel_opts_name).all():
sub_obj._collect_sub_objects(seen_objs)

Adding an 'hasattr' test got rid of the error:

if hasattr(self, rel_opts_name):
for sub_obj in getattr(self, rel_opts_name).all():
sub_obj._collect_sub_objects(seen_objs)

Now, does that look like a bug to you? If so, is my proposed fix
appropriate? If not, have I missed something?

Cheers,

Julien

Karen Tracey

unread,
Jun 2, 2008, 9:39:02 AM6/2/08
to django-d...@googlegroups.com

Do you get the error only if you delete via the Admin?  If so then I think this falls under the umbrella of "Admin doesn't currently support model inheritance", see:

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

If you can recreate the error without Admin, then that's a different issue.  I'd search the ticket tracker to see if there are any other reports and if not report it.

Karen

Julien

unread,
Jun 2, 2008, 6:18:06 PM6/2/08
to Django developers
Hi Karen,

I confirm that I get the error both in the admin and in front-end
views.

Here the example there is a combination of things that makes the
situation a bit complex. Indeed, the 2 abstract models (FileGalleried
and Avatared) both have a reference to a same model (FileGallery).
Because the class Project inherits from those 2 abstract models, I had
to add a 'related_name' attribute containing '%(class)s'.

I don't understand why, when you delete the object, it assumes that it
does have related objects accessible via the attributes
'project_related' and 'userprofile_related'. The fact is that it may
not have those attributes, hence the suggested 'hasattr' test.

What do you think?

On Jun 2, 11:39 pm, "Karen Tracey" <kmtra...@gmail.com> wrote:

mattim...@gmail.com

unread,
Jun 2, 2008, 8:03:23 PM6/2/08
to Django developers
Hi Julien,


On Jun 3, 8:18 am, Julien <jpha...@gmail.com> wrote:
> Hi Karen,
>
> I confirm that I get the error both in the admin and in front-end
> views.
>
> Here the example there is a combination of things that makes the
> situation a bit complex. Indeed, the 2 abstract models (FileGalleried
> and Avatared) both have a reference to a same model (FileGallery).
> Because the class Project inherits from those 2 abstract models, I had
> to add a 'related_name' attribute containing '%(class)s'.

There is a known issue with related_name and '%(class)s' not working.
There is a patch in this ticket http://code.djangoproject.com/ticket/7215
.

regards

Matthew

Reply all
Reply to author
Forward
0 new messages