Django - Am I in the right direction for creating the models of my Phone Review application?

Viewed 95

I am a beginner in Django. I am building a data model for a Django app, named PhoneReview. It will store reviews related to the latest mobile phone. It's table should include:

a. Brand – details on brand, such as, name, origin, manufacturing since, etc

b. Model – details on model, such as, model name, launch date, platform, etc

c. Review – review article on the mobile phone and date published, etc

d. Many-to-many relationship between Review and Model.

Here are my codes in models.py:

from django.db import models
from django.template.defaultfilters import slugify

# Create your models here.
class Brand(models.Model):
    brandName = models.CharField(max_length=100)
    origin = models.CharField(max_length=100)
    manufacturingSince = models.CharField(max_length=50, default='null')
    def __str__(self):
        return self.brandName

class PhoneModel(models.Model):
    modelName = models.CharField(max_length=100)
    launchDate = models.CharField(max_length=100)
    platform = models.CharField(max_length=100)
    def __str__(self):
        return self.modelName

class Review(models.Model):
    model_name_many_to_many = models.ManyToManyField(PhoneModel)
    reviewArticle = models.CharField(max_length=1000)
    datePublished = models.DateField(auto_now=True)
    slug = models.SlugField(max_length=150, default='null')
    def __str__(self):
        return self.reviewArticle

Are my codes correct? Am I in the right direction?

1 Answers

Don't use camelCase in model fields. Use snake_case. Second thing is, when you want field to be default 'null', just use null=True, blank=True(optional value). I've also provided related_name to your ManyToManyField, so you can use PhoneModelInstance.reviews.all() to get your all reviews for this specific Phone model. For large fields containing text, use TextField.

Edit

I've also added foreign key in PhoneModel which points to the Brand.

from django.db import models
from django.template.defaultfilters import slugify

# Create your models here.
class Brand(models.Model):
    brand_name = models.CharField(max_length=100)
    origin = models.CharField(max_length=100)
    manufacturing_since = models.TextField(null=True, blank=True)

    def __str__(self):
        return self.brand_name

class PhoneModel(models.Model):
    brand_fk = models.ForeignKey(Brand)
    model_name = models.CharField(max_length=100)
    launch_date = models.CharField(max_length=100)
    platform = models.CharField(max_length=100)

    def __str__(self):
        return self.model_name

class Review(models.Model):
    phone_model = models.ManyToManyField(PhoneModel, related_name='reviews')
    review_article = models.TextField()
    date_published = models.DateField(auto_now=True)
    slug = models.SlugField(max_length=150, null=True, blank=True)

    def __str__(self):
        return self.review_article
Related