from django.db import models

# Represents a category which articles can be part of
class Category(models.Model):
    name = models.CharField(max_length=255)

    def __str__(self):
        return self.name+" ["+str(self.id)+"]"


# Article model represents one article in the blog.
class Article(models.Model):
    title = models.CharField(max_length=255)
    slug = models.SlugField(max_length=255)
    contents = models.TextField()
    date_published = models.DateTimeField()
    published = models.BooleanField()
    categories = models.ManyToManyField(Category)
    
    def __str__(self):
        return self.title+" ["+str(self.id)+"]"



from django.db import models

# Represents a category which articles can be part of
class Category(models.Model):
    name = models.CharField(max_length=255)

    def __str__(self):
        return self.name+" ["+str(self.id)+"]"


# Article model represents one article in the blog.
class Article(models.Model):
    title = models.CharField(max_length=255)
    slug = models.SlugField(max_length=255)
    contents = models.TextField()
    date_published = models.DateTimeField()
    published = models.BooleanField()
    primary_category = models.ForeignKey(Category, related_name='+')
    categories = models.ManyToManyField(Category)
    
    def __str__(self):
        return self.title+" ["+str(self.id)+"]"



Add a primary category which will decide which category is shown as current in the chrome for this article.
Add a primary category which will decide which category is shown as current in the chrome for this article.

TODO: Enforce including the primary category as one of the categories for the article, both in UI and server side.

  from django.db import models
  
  # Represents a category which articles can be part of
  class Category(models.Model):
      name = models.CharField(max_length=255)
  
      def __str__(self):
          return self.name+" ["+str(self.id)+"]"
  
  
  # Article model represents one article in the blog.
  class Article(models.Model):
      title = models.CharField(max_length=255)
      slug = models.SlugField(max_length=255)
      contents = models.TextField()
      date_published = models.DateTimeField()
      published = models.BooleanField()
+     primary_category = models.ForeignKey(Category, related_name='+')
      categories = models.ManyToManyField(Category)
      
      def __str__(self):
          return self.title+" ["+str(self.id)+"]"
  
  
  
Add a primary category which will decide which category is shown as current in the chrome for this article.
## Code Before:
from django.db import models

# Represents a category which articles can be part of
class Category(models.Model):
    name = models.CharField(max_length=255)

    def __str__(self):
        return self.name+" ["+str(self.id)+"]"


# Article model represents one article in the blog.
class Article(models.Model):
    title = models.CharField(max_length=255)
    slug = models.SlugField(max_length=255)
    contents = models.TextField()
    date_published = models.DateTimeField()
    published = models.BooleanField()
    categories = models.ManyToManyField(Category)
    
    def __str__(self):
        return self.title+" ["+str(self.id)+"]"



## Instruction:
Add a primary category which will decide which category is shown as current in the chrome for this article.
## Code After:
from django.db import models

# Represents a category which articles can be part of
class Category(models.Model):
    name = models.CharField(max_length=255)

    def __str__(self):
        return self.name+" ["+str(self.id)+"]"


# Article model represents one article in the blog.
class Article(models.Model):
    title = models.CharField(max_length=255)
    slug = models.SlugField(max_length=255)
    contents = models.TextField()
    date_published = models.DateTimeField()
    published = models.BooleanField()
    primary_category = models.ForeignKey(Category, related_name='+')
    categories = models.ManyToManyField(Category)
    
    def __str__(self):
        return self.title+" ["+str(self.id)+"]"


