3

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?

Shawn
  • 173
  • 14

1 Answers1

1

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
Community
  • 1
  • 1
token
  • 903
  • 1
  • 9
  • 23
  • Thanks for the answer. However, I have a question regarding phone_model = models.ManyToManyField(PhoneModel, related_name='reviews'). So, basically, this line allows me to use all the variables of PhoneModel class in the Review class. Am I right? And what is the purpose of related_name='reviews' ? – Shawn Oct 16 '19 at 19:11
  • 2
    Review instance example: review_obj.phone_model.all() - returns all PhoneModel objects related to this review. PhoneModel instance example: phone_model_obj.reviews.all() - returns all Review objects related with this PhoneModel. For related name explaination, check https://stackoverflow.com/questions/2642613/what-is-related-name-used-for-in-django – token Oct 17 '19 at 05:40