Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#28563 closed New feature (wontfix)

Allow parent relations to specify the related_name of a class property

Reported by: Samuel Spencer Owned by: tothegump
Component: Uncategorized Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by Samuel Spencer)

I can't come up with an appropriate example, so I'll use a cut back version of actual code. I'm trying to implement an ISO standard, and am using a concrete base class, with a number of relations between objects, as shown below. Unfortunately, the names of these objects and relations are defined by the spec and can't be changed without a lot of hassle.

	
class Metadata(models.Model):
    name = models.CharField(max_length=255)
	
class Property(Metadata):
    pass
	
class ObjectClass(Metadata):
    pass
	
class DataElementConcept(Metadata):
    object_class = models.ForeignKey(ObjectClass)
    property= models.ForeignKey(Property)
	


However, the system checker has a problem with this:

SystemCheckError: System check identified some issues:
	
ERRORS:
aristotle_mdr.DataElementConcept.property: (models.E006) The field 'property' clashes with the field 'property' from model 'aristotle_mdr._concept'.


What appears to happen is that DataElementConcept.property (a relation between two pieces of metadata) is clashing with MetadataItem.property (a relation from parent class to child class).

Since a parent-child relationship using inheritance is just a OneToOneField thats made by django.db.models.base.ModelBase it'd be nice to be able to add some details to a classes Meta field to change the related_name of the generated field, for example:

	
class Metadata(models.Model):
    name = models.CharField(max_length=255)
	
class Property(Metadata):
    class Meta:
        parent_related_name = "property_subclass"
	


and the ModelBase coude would be similar to:

                    field = OneToOneField(
                        base,
                        on_delete=CASCADE,
                        name=attr_name,
                        auto_created=True,
                        parent_link=True,
                        related_name=base._meta.parent_related_name
                    )
}}

Change History (5)

comment:1 by Simon Charette, 7 years ago

I'm not convinced it's worth adding a Meta option for the rare cases when this is required. Explicitly defining your parent link as you've pointed out should do.

class Property(Metadata):
    metadata_ptr = OneToOneField(
        Metadata,
        on_delete=CASCADE,
        parent_link=True,
        related_name='property_subclass',
    )

comment:2 by tothegump, 7 years ago

Owner: changed from nobody to tothegump
Status: newassigned

comment:3 by Tim Graham, 7 years ago

Resolution: wontfix
Status: assignedclosed

Agreed, I think defining fields is a more clear solution than a Meta option.

comment:4 by Samuel Spencer, 7 years ago

Description: modified (diff)

comment:5 by Samuel Spencer, 7 years ago

Description: modified (diff)

I didn't even realise that was possible! Cheers.

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