I have two models that are related with many to many relationship. I need the data of the second model to be stored in the database when creating the object from the first model so that later if the values of the second model change, it will not affect the values we entered in the first model. How should I act?
# models.py
class First(models.Model):
percent = models.IntegerField()
two = models.ManyToManyField(Two, blank=True)
class Two(models.Model):
title = models.CharField(max_length=50, null=True)
amount = models.PositiveIntegerField(null=True)
# forms.py
class FirstAddForm(forms.ModelForm):
class Meta:
model = First
def save(self, commit=True):
instance = super(FirstAddForm, self).save(commit=False)
instance.percent = self.cleaned_data['percent'] / 100
.
# How should I act? #
.
if commit:
instance.save()
self.save_m2m()
return instance
# views.py
def FishDetailView(request, pk=None):
first = First.objects.all()
.
.
for i in First:
two= Two.objects.filter(first__id=i.id)
.
.
context = {
'first': first
'two': two,
}