Code

Opened 8 years ago

Closed 8 years ago

Last modified 3 years ago

#2318 closed enhancement (wontfix)

Hibernate's component mapping type for Django

Reported by: anonymous Owned by: adrian
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)

Attachments (0)

Change History (3)

comment:1 Changed 8 years ago by adrian

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

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

comment:2 Changed 8 years ago by jim-django@…

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

comment:3 Changed 3 years ago by anonymous

  • 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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.