Wednesday, February 22, 2017

Models/Foreign Keys best practice

Hi all,

I'm putting together my first app, closely modelled on the Django tutorial, and wanted some advice regarding when to split the various models into different foreign keys.

The app lists performances, who's performing, what my role is with them (i.e. conductor, pianist etc.), the venue and the start date/time of these performances.

I'd initially thought I should split these all into separate classes, since, for example, the same Ensemble might be involved in a different performance, but I'm now not sure that's correct (/my understanding is incorrect). Would someone mind letting me know if this is a good idea or whether there's a better practice I should follow.

I've included my models.py file below:

from django.db import models
from django.utils import timezone
# Create your models here.


class Performance(models.Model):
    """First attempt at a thing!"""
    performance_name = models.CharField(max_length=200)
    link = models.CharField(max_length=200)

    def __unicode__(self):
        return self.performance_name


class Ensemble(models.Model):
    performance = models.ForeignKey(Performance)
    ensemble = models.CharField(max_length=200)

    def __unicode__(self):
        return self.ensemble


class Role(models.Model):
    performance = models.ForeignKey(Performance)
    role = models.CharField(max_length=200)

    def __unicode__(self):
        return self.role


class Venue(models.Model):
    performance = models.ForeignKey(Performance)
    venue = models.CharField(max_length=200)

    def __unicode__(self):
        return self.venue


class Date(models.Model):
    performance = models.ForeignKey(Performance)
    start_date = models.DateTimeField('Start Date', default=timezone.now)
    end_date = models.DateTimeField('End Date', default=timezone.now)


Thanks!

Rich

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b3d1402b-6a70-4538-a0ef-3e895cf7a51e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment