[Django]-How can we add more than 3 authors field for a single magzine? in Django models

3👍

You can make an extra model Author with a ForeignKey [Django-doc] to Magazine. If the same Author can write multiple Magazines, you might want to use a ManyToManyField [Django-doc] instead:

class Author(models.Model):
    name = models.CharField(max_length=300, unique=True)
    designation = models.CharField(max_length=300, blank=True)
    picture = models.ImageField(upload_to_authorpic, blank=True)
    detail = model.TextField(max_length=1000, blank=True)

class Magazine(models.Model):
    # …
    authors = models.ManyToManyField(
        Author,
        related_name='magazines',
        on_delete=models.CASCADE
    )

You probably want to make name a unique=True [Django-doc] field to avoid creating two Author objects with the same name.

Leave a comment