I have next model:
class People(models.Model):
name = models.CharField(max_length=100)
lastname = models.CharField(max_length=100)
class Salary(models.Model):
id_of_people=models.ForeignKey(People)
salary = models.IntegerField(required=False)
In views.py
-When I try this one:
for each in People.objects.all()[:3]:
Salary().id_of_people_id=each.id
Salary().save()
It only saves 3 Th id, not from 1 to 3. So, it only saves one time. But, I want to loop through all 3 records, why this happens ? How to add all People table id’s to Salary table ?
You’re creating a single Salary object and updating it three times. You need to create three separate objects.