Fred Collins
Fred Collins

Reputation: 5010

Database design for Django review website

Hi I'm developing a review site for learning Django.

Every review has a list of rating attributes like:

I'm stuck at the point of writing the review/rating model. How can I describe this with MVC pattern? The list of rating attributes can be different.

Currently I've this code:

class Place(models.Model):
    name = models.CharField(max_length=512)
    address = models.CharField(max_length=512)
    author = models.ForeignKey(UserProfile)

class Review(models.Model):
    ????

class Review(models.Model):
    name = models.CharField(max_length=1024)
    text = models.TextField()
    author = models.ForeignKey(UserProfile)
    place = models.ForeignKey(place)
    ratings = models.ForeignKey(Rating)

Any hint?

Upvotes: 1

Views: 1721

Answers (1)

Jan Pöschko
Jan Pöschko

Reputation: 5570

If the list of (potential) attributes is available beforehand, you can just introduce a database field for each of them and store not-set attributes as None:

class Review(models.Model):
    name = models.CharField(max_length=1024)
    text = models.TextField()
    author = models.ForeignKey(UserProfile)
    place = models.ForeignKey(place)

    price_rating = models.IntegerField(null=True)
    location_rating = models.IntegerField(null=True)
    # ...

Otherwise (if the attributes/ratings are totally variable), you have to create another model to store your attributes (drop the _rating fields above then):

class ReviewAttribute(models.Model):
    review = models.ForeignKey(Review, related_name='attributes')
    name = models.CharField(max_length=100)
    value = models.IntegerField()

Upvotes: 2

Related Questions