Opened 3 years ago

Closed 3 years ago

#19561 closed Uncategorized (invalid)

Through-table model attributes are not available

Reported by: qcwxezdas@… Owned by: nobody
Component: Database layer (models, ORM) Version: 1.5-beta-1
Severity: Normal Keywords: orm, many-to-many, through
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Here's my code:

class Property(models.Model):
    title = models.CharField(_("Title"), max_length=150, unique=True)
    default_description = models.TextField(_("Default description"), blank=True)

class AccommodationProperty(models.Model):
    accommodation = models.ForeignKey("Accommodation")
    prop = models.ForeignKey(Property, verbose_name=_("Property"),
                             db_column="property")

    custom_description = models.TextField(_("Custom description"), blank=True)

class Service(models.Model):
    title = models.CharField(_("Title"), max_length=150)

class Accommodation(Service):
    properties = models.ManyToManyField(Property, through=AccommodationProperty,
                                        related_name="accommodations", blank=True)

And here's what's wrong:

>>> acprop = AccommodationProperty.objects.all()[0]
>>> acprop.accommodation
<Accommodation: test apgyv>
>>> acprop.prop
<Property: Internetas>
>>> acprop.custom_description
u'customas'
>>> acprop.prop.default_description
u'defaultas'
>>> ac = acprop.accommodation
>>> ac.properties.all()
[<Property: Internetas>]
>>> prop = ac.properties.all()[0]
>>> prop.default_description
u'defaultas'
>>> prop.title
u'Internetas'
>>> prop.custom_description
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'Property' object has no attribute 'custom_description'
>>> 

Attribute custom_description cannot be accessed.

Change History (2)

comment:1 Changed 3 years ago by qcwxezdas@…

  • Component changed from ORM aggregation to Database layer (models, ORM)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 3 years ago by ramiro

  • Resolution set to invalid
  • Status changed from new to closed

In your sample code, prop is an instance of the Property model. and Property has no custom_description field.

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