Django

Code

Ticket #7215 (closed: fixed)

Opened 2 months ago

Last modified 2 weeks ago

related_name argument for ForeignKey ignored when inheriting from abstract base class

Reported by: jpwatts Assigned to: jpwatts
Milestone: 1.0 Component: Core framework
Version: SVN Keywords: qsrf-cleanup model inheritance related
Cc: joel@joelwatts.com Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

When using a ForeignKey with the related_name argument in an abstract base class, the reverse accessors for child models aren't created.

The problem can be reproduced using the following models:

import datetime

from django.db import models

class Post(models.Model):
    title = models.CharField(u'Title', max_length=50)

class Attachment(models.Model):
    post = models.ForeignKey(Post, related_name='attached_%(class)s_set')
    content = models.TextField(u'Content')

    class Meta:
        abstract = True

class Comment(Attachment):
    is_spam = models.BooleanField(u'Spam')

class Update(Attachment):
    timestamp = models.DateTimeField(default=datetime.datetime.now)

Given those models, I would expect Post to have two accessors for related models: Post.attached_comment_set and Post.attached_update_set, but it doesn't. In their place is a single accessor named Post.attached_%(class)s_set.

I'm attaching a patch that rearranges django.db.models.fields.related.RelatedField.contribute_to_class enough to get the accessor methods, but I didn't see where the attached_%(class)s_set attribute was being created.

Attachments

related_name.diff (3.2 kB) - added by jpwatts on 05/10/08 23:38:56.
changes to RelatedField to fix related_name problem (with tests)

Change History

05/10/08 23:25:21 changed by jpwatts

  • needs_better_patch set to 1.
  • needs_tests changed.
  • needs_docs changed.

I added tests to my patch. They check to see that the accessors for the related models are created and that the attached_%(class)s_set attribute isn't created.

The changes I made to RelatedField satisfy the requirement that the accessors be created, but do not prevent the extraneous attribute from begin added.

It appears that attached_%(class)s_set thinks it's an accessor for Attachment (the abstract base class) as the test fails with these results:

Traceback (most recent call last):
    ...
AttributeError: type object 'Attachment' has no attribute '_default_manager'

05/10/08 23:38:56 changed by jpwatts

  • attachment related_name.diff added.

changes to RelatedField to fix related_name problem (with tests)

05/10/08 23:46:15 changed by jpwatts

  • owner changed from nobody to jpwatts.
  • needs_better_patch deleted.
  • status changed from new to assigned.

Turns out I was just one if statement away from meeting all the requirements. The patch now sets the correct accessor attributes and doesn't try to create one for abstract models.

06/10/08 10:03:56 changed by gav

  • keywords changed from model inheritance related to qsrf-cleanup model inheritance related.

06/12/08 08:53:56 changed by julien

I confirm that the patch also fixes a related issue that I was experiencing as described in: http://groups.google.com/group/django-developers/browse_thread/thread/97836d6913a311db/dae5e8110139cfc9?lnk=gst&q=julien#dae5e8110139cfc9

06/16/08 12:10:45 changed by jacob

  • milestone set to 1.0.

06/17/08 15:05:44 changed by Jurian Botha <jurianbotha@gmail.com>

  • stage changed from Unreviewed to Ready for checkin.

I've tried this patch out and all seems fine to me. The tests also look up to scratch.

06/26/08 01:39:26 changed by mtredinnick

Looks like you've understood everything, jpwatts. Nice patch.

06/26/08 02:04:23 changed by mtredinnick

  • status changed from assigned to closed.
  • resolution set to fixed.

(In [7762]) Fixed #7215 -- Create correct reverse-relation accessors when using abstract base classes. Patch from Joel Watts.


Add/Change #7215 (related_name argument for ForeignKey ignored when inheriting from abstract base class)




Change Properties
Action