DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Model Templates in Django without Denormalization

Model Templates in Django without Denormalization

Rob Golding user avatar by
Rob Golding
·
Feb. 05, 12 · Web Dev Zone · Interview
Like (0)
Save
Tweet
3.53K Views

Join the DZone community and get the full member experience.

Join For Free

I came across an interesting problem recently, while trying to model the structure of a university course in Django.

The model needed to represent the notion of a university module, which can be taught over a number of semesters and/or years, by different people, and with different students each time round. Some information remained common to each of these modules however, such as the code, name, and type of the module (single semester or full-year).

One solution that crossed my mind was to use a sort of template, and copy the common information over each time the module was taught. This would mean duplicating the common fields over a template model and the module’s model, and copying the data every time a new “Module” object gets created. This just didn’t sit well with me though, as I really don’t like denormalising data when it’s not absolutely necessary.

The solution I have settled on (for now, anyway) works something like this: I use the model template idea, but with a foreign key from the Module to it’s template. Then, to access the fields on the template directly from the Module model, I use a combination of the Python property and a smart lambda function.

Here’s a simplified version of the model to illustrate what I’m talking about:

models.py

class ModuleDefinition(models.Model):
code = models.CharField(max_length=6, unique=True)
name = models.CharField(max_length=200)
credits = models.IntegerField(max_length=4)
level = models.IntegerField(max_length=2)
type = models.CharField(max_length=30, choices=MODULE_TYPE_CHOICES)
class Module(models.Model):
code = property(lambda s: s._get_definition_property('code'))
name = property(lambda s: s._get_definition_property('name'))
credits = property(lambda s: s._get_definition_property('credits'))
level = property(lambda s: s._get_definition_property('level'))
definition = models.ForeignKey('ModuleDefinition')
semester = models.CharField(max_length=20, choices=SEMESTER_CHOICES)
year = models.CharField(max_length=20, choices=YEAR_CHOICES)
convener = models.ForeignKey('auth.User', related_name='modules_convened')
students = models.ManyToManyField('auth.User', blank=True, related_name='modules_taken')
def _get_definition_property(self, property):
return self.definition.__getattribute__(property) 


In this model I’ve called the template a “definition”, but the idea is the same. The _get_definition_property() method allows me to get a property from the parent definition programmatically, which is used in the lambda functions to add the fields from the definition to the model itself, as read-only properties. This allows me to access them as if they were a field stored on the model itself (i.e. module.code, module.name, etc.) which is handy in the templates where module.definition.code would get very old, very fast.

I’d be really interested to hear how others have tackled the same issue, as I’m sure it’s not just restricted to university modules. Maybe soon I’ll be making a post about how I model a timetable in Django…after all, how hard can it be!?

Oh, and if you’re wondering, this code is for the MyUni project that is occupying me recently. I’m restricting myself to just thinking about how the models could work (and maybe writing little bit of code) until Rob and Ben come back to university.


Source: http://www.robgolding.com/blog/2010/04/27/django-model-templates-sans-denormalisation/

Template Django (web framework)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • When Writing Code Isn't Enough: Citizen Development and the Developer Experience
  • Purpose-Driven Microservice Design
  • Spring, IoC Containers, and Static Code: Design Principles
  • Practice on Pushing Messages to Devices of Different Manufacturers

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo