Saturday, December 29, 2012

Re: Design by composition and persistance

On Sat, Dec 29, 2012 at 3:39 AM, Taras_96 <taras.di@gmail.com> wrote:
> If we map software objects directly onto Django Models, then we'd end up
> with a TimeWindow table, where each row would only ever be referenced by
> exactly one Event. My understanding of the advantage of normalisation is
> that it prevents data duplication, and thus helps with data consistency.
> However, it doesn't really make much sense to incur the cost of this
> (through joins) if the object that is being modelled is a value object.

there's value in one-to-one relationships too. it's not about
performance, but about data organization. also, if there's a lot of
data that logically 'belongs' to a record but is seldom used, it's
easier to ignore when not needed if it's on a separate table.

but if it really belongs to the record, and you want to store on the
same table, and keep it conceptually as a separate object, then it
could be easier to create a new field class.

it could be either a full-blown field that stores its data on more
than one DB field, or it could be an extra abstraction used on top of
existing (and declared) fields, similar to the way generic
relationships work
(https://docs.djangoproject.com/en/1.4/ref/contrib/contenttypes/#generic-relations):


--------------------------
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic

class TaggedItem(models.Model):
tag = models.SlugField()
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')

def __unicode__(self):
return self.tag
---------------------------

there you see the 'content_type' and 'object_id' fields store some
'low-level' data, and the 'content_object' field uses those to present
a different interface: the relationship behaviour.

in your case:

class Event(models.Model):
name = models.CharField(.....)
description = models.CharField(....)
tw_start = models.DateTimeField(....)
tw_finish= models.DateTimeField(....)
timewindow = TimeWindowField(tw_start, tw_finish)

and getDuration() is a method of the TimeWindowField, so you can say
event.timewindow.getDuration()


--
Javier

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment