I'm trying to save data in loops from django admin. As i press it should save in database with same argument except one field which will have different value in each row.
class DealersList(models.Model):
dealers_company = models.CharField(unique=True,max_length=100)
concern_district = models.CharField(max_length=25, choices=CITY_CHOICES, default=False)
address = models.CharField(max_length=50)
vdc = models.CharField(max_length=30)
contact_person = models.CharField(max_length=50,blank=True)
phone_number = models.CharField(max_length=14,blank=True)
email = models.EmailField(max_length=70,blank=True, null= True, unique= True)
And my SimDetail class which will have save() function contains a ForeignKey too
class SimDetail(models.Model):
mobile_no = models.BigIntegerField("Mobile Number",unique=True)
number_of_sim = models.IntegerField()
agent = models.ForeignKey('DealersList',on_delete=models.CASCADE,to_field='dealers_company')
sim_activation_date = models.DateField(auto_now=False, auto_now_add=False)
submission_date = models.DateField(auto_now=False, auto_now_add=False)
remarks = models.TextField(null=True,blank=True,validators=[MaxLengthValidator(200)])
def save(self,*args, **kwargs):
ite=0
for x in xrange(0,self.number_of_sim):
self.mobile_no=self.mobile_no+ite
ite=1
super(SimDetail, self).save(*args, **kwargs)
Currently it's only saving last data which i think(i may be wrong) might being replaced again and again so at last it only stores the last value.
Can anyone help me, i'm trying to use it from admin only without using forms.