Wednesday, February 22, 2017

Re: Models/Foreign Keys best practice

Hi Richard,

I am facing a difficulty here.
Can you please tell me that how to iterate over this queryset , performance.ensemble_set.all()  ??

Thanks.

On Wednesday, February 22, 2017 at 7:55:24 PM UTC+5:30, Richard Jackson wrote:
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/d998078b-dd87-43a6-ac6c-1cb5b120f135%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment