Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Override Save to create multiple objects for range()

I want to override Django form save method to be able to create multiple objects at once.

I have an AvailableHours model :

class AvailableHours(models.Model):
    free_date = models.ForeignKey(AvailableDates,null=True, blank=True,on_delete=models.CASCADE,related_name='freedate')
    free_hours_from = models.IntegerField(null=True, blank=True)
    free_hours_to = models.IntegerField(null=True, blank=True,)
    status = models.BooleanField(null=True,default=True,)

and I have a ModelForm for this class :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class TimeGenerator(forms.ModelForm):
    class Meta:
        model = AvailableHours

        fields = ['free_date','free_hours_from','free_hours_to','status']

    def save(self, commit=True):
        m = super(TimeGenerator, self).save(commit=False)
        self.free_hours_from = self.cleaned_data['free_hours_from']
        self.free_hours_to = self.cleaned_data['free_hours_from'] +1
        m.save()
        for hour in range(self.cleaned_data['free_hours_from'],self.cleaned_data['free_hours_to']+1):
            self.free_hours_from = self.cleaned_data['free_hours_from']+hour
            self.free_hours_to = self.cleaned_data['free_hours_from']+hour+1
            print(hour)
            m.save()

I want to create multiple AvailableHours object like this:
if the form is set free_hours_from = 13 and free_hours_to=16
so the save method creates 13-14 , 14-15, 15-16

I wrote a code above in forms and overrided save() but its not working.
anyone has a solution ?

>Solution :

You can create items and save these in bulk with:

class TimeGenerator(forms.ModelForm):
    class Meta:
        model = AvailableHours

        fields = ['free_date', 'free_hours_from', 'free_hours_to', 'status']

    def save(self, commit=True):
        base_item = super().save(commit=False)
        frm = self.cleaned_data['free_hours_from']
        to = self.cleaned_data['free_hours_to']
        items = [
            AvailableHours(
                free_date=base_item.free_date,
                status=base_item.status,
                free_hours_from=hour,
                free_hours_to=hour + 1,
            )
            for hour in range(frm, to)
        ]
        if commit:
            AvailableHours.objects.bulk_create(items)
        return items

That being said, it is odd that the AvailableHours has a free_hours_to field if that is always the free_hours_from plus one.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading