2

I have a Table Books which has 100 entries.

class Books(models.Model):
    name = models.CharField(max_length=30)

I have another Table Titles which is newly created and has a Foreign Key relation.

class Titles(models.Model):
    book = models.ForeignKey(Books, on_delete=models.CASCADE)
    ..........

Can I automatically create 100 new entries for Titles based on the existing entry for Books?

Or does changing Foreign Key to a different relation help?

Please help me out or point me in the right direction. Thank you in advance.

The Doctor
  • 486
  • 1
  • 6
  • 16

1 Answers1

2

I don't know about creating them automatically, but you can certainly create them quite easily.

Open a django shell with python manage.py shell and do something like:

from my_app.models import Books, Titles

for book in Books.objects.all():
    new_title = Titles.objects.create(Books=book, ...)

P.S. The model naming convention would be 'Book' not 'Books' and 'Title' not 'Titles'.

FiddleStix
  • 3,016
  • 20
  • 21