Randy Tang
Randy Tang

Reputation: 4353

Django models: foriegn key or multiple data in a field

Actually, this question has puzzled me for a long time.

Say, I have two models, Course and CourseDate, as follows:

class Course(models.Model):
    name = model.CharField()

class CourseDate(models.Model):
    course = modelds.ForienKey(Course)
    date = models.DateField()

where CourseDate is the dates that a certain course will take place.

I could also define Course as follows and discard CourseDate:

class Course(models.Model):
    name = models.CharField()
    dates = models.CharField()

where the dates field contains dates represented by strings. For example:

dates = '2016-10-1,2016-10-12,2016-10-30'

I don't know if the second solution is kind of "cheating". So, which one is better?

Upvotes: 0

Views: 20

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599610

I don't know about cheating, but it certainly goes against good database design. More to the point, it prevents you from doing almost all kinds of useful queries on that field. What if you wanted to know all courses that had dates within two days of a specific date? Almost impossible to do that with solution 2, but simple with solution 1.

Upvotes: 2

Related Questions