简体   繁体   中英

Is it possible to create a new row in models.py for a list of items in django?

I'm trying to create a todo app, and I want to have a CharField in my models.py for each todo item. Here's my code right now:

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    todo = models.CharField(max_length=1000, default='')

    def __str__(self):
        return f'{self.user.username} Profile'

I want to make it so a new row in the database is created for each new todo. For example, there would be a row for one task, and another row for another task. How would I go about this? Thanks in advance!

Add additional model(table) for ToDo and connect it with many-to-one relationship

Class ToDo(models.Model):
      user = models.ForeignKey('User', on_delete=models.CASCADE)
      todo = models.CharField(max_length=1000, default='')

You should probably want to read a bit more about database relationships

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM