0

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.

ashim888
  • 206
  • 10
  • 22
  • I just found that some seconds before you posted, would have been much help if i had got that earlier. Thanks @smarber – ashim888 Jul 25 '17 at 11:03
  • You found it, I didn't :). I saw your answer so I flagged it as duplicate, if it's mainly the same question as yours you should do the same... – smarber Jul 25 '17 at 11:06
  • yeah the answer was applicable to my problem too and i accepted its duplicate plus upvoted the given answer :) – ashim888 Jul 25 '17 at 11:11

1 Answers1

0

All i needed to do was just put primary key to None so that a new object will be saved each time, i found my answer in this link

ashim888
  • 206
  • 10
  • 22