Home > Net >  Can I create Django models automatically?
Can I create Django models automatically?

Time:11-26

I'm working on a resume website with Django in which, for the skill section I defined a model as below,

from django.db import models


class Skills(models.Model):
    name = models.CharField(max_length=50)
    level = models.DecimalField(max_digits=3, decimal_places=3)
    def __str__(self):
        return self.name

But, for example, if I add a skill "Python" through admin in this model, I want to add a few slides for the skill, each containing a image and a paragraph, to give detailed information about my skill.

In other words, I want to create a table for each skill I'll add with columns named image, and description.

Is there any way I can achieve it?

CodePudding user response:

If one skill can have multiple 'slides', you need to create another model with a foreignkey(ManyToOne) to your skills model.

for example:

class Skills(models.Model):
    name = models.CharField(max_length=50)
    level = models.DecimalField(max_digits=3, decimal_places=3)
    def __str__(self):
        return self.name

class SkillDetail(models.Model):
    skill = models.ForeignKey(Skills, on_delete=models.CASCADE)
    image = models.ImageField(upload_to='images/')
    description = models.TextField()
  • Related