Opened 18 years ago

Closed 18 years ago

Last modified 13 years ago

#2318 closed enhancement (wontfix)

Hibernate's component mapping type for Django

Reported by: anonymous Owned by: Adrian Holovaty
Component: Database layer (models, ORM) Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Something similar to component mapping type of Hibernate (http://www.hibernate.org/hib_docs/v3/reference/en/html/components.html) would be a great addition to Django's model API.

Currently, the same behaviour can be achieved by using an one-to-one relation between two models, but 1) one-to-one isn't recommended, 2) it's a bit overkill for simple models. Let's say we have model Product. Naturally, we'd like to have a price for the product. To be as object-oriented as possible, we would like to model the price as MonetaryAmount, which would contain the following information: the amount of money and the currency it is in.

With one-to-one models, we could model this as follows:

class Product(models.Model):
  ...

class MonetaryAmount(models.Model):
  money_amount = models.FloatField()
  currency = models.CharField()
  product = models.OneToOneField(Product)

This works, but as mentioned, it's a bit overkill: another table in database just to hold information about amount of money and the currency it is in and the need for extra-JOINs when retrieving database records.

With component mapping the fields introduced in MonetaryAmount would be mapped at database to the same table with Product and therefore no additional tables nor JOINs would be needed. This could be modeled, for example, as:

class MonetaryAmount(object):
  money_amount = models.FloatField()
  currency = models.CharField()
  
class Product(models.Model):
  ...
  price = models.ComponentField(MonetaryAmount)

Change History (3)

comment:1 by Adrian Holovaty, 18 years ago

Resolution: wontfix
Status: newclosed

I'm -0 on this -- it seems like syntactic sugar for something that Django already supports.

comment:2 by jim-django@…, 18 years ago

This usage is covered by the abstract model portions of the recent model inheritance proposal.

comment:3 by anonymous, 13 years ago

Easy pickings: unset
UI/UX: unset

Abstract model does not look as though it is equivalent to Hibernate components.

Using the OP's example, you would reference the currency field by "Product.price.currency" with a Hibernate component. With a Django abstract model you would have "Product.currency". With a OneToOneField you would have "Product.price.currency" but then you have an extra undesirable join.

I am fairly new to Django so please correct me if I am wrong.

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