I am new to Django facing this error while saving data into DB:
"ValueError at /breedingRecApp/pairing
Cannot assign "<SpeciesDetail: Black Mask Yellow Chest>": "Pair.male_mutation" must be a "Mutation" instance."
I've 4 foreign keys in Pair model each with specific reason/purposes. when I save data then error come.
in the same error page Form Valid is TRUE and Data What I want is also exactly the same. I don't know why data is not saving in DB.
Note: When I insert the same data from Admin interface it get save without any error it also get updated and delete as well from admin interface.
following are models:
models.py
class Mutation(models.Model):
species = models.ForeignKey(Species, on_delete=models.CASCADE)
name = models.CharField(max_length=120, blank=False, null=False)
description = models.TextField(max_length=300, blank=False, null=False)
class Meta:
verbose_name_plural = "Mutations"
ordering = ('name',)
def __str__(self):
return
self.name def get_absolute_url_delete(self):
return reverse("breedingRecApp:delete_mutation", kwargs={"mutation_id":
self.pk})
class SpeciesDetail(models.Model):
GENDER_CHOICES = (
('M', 'Male'),
('F', 'Female'),
)
species = models.ForeignKey(Species, on_delete=models.CASCADE)
mutation = models.ForeignKey(Mutation, on_delete=models.CASCADE)
fatherName = models.CharField(max_length=120)
motherName = models.CharField(max_length=120)
birdBandNo = models.IntegerField()
cageNo = models.IntegerField()
picture = models.ImageField(upload_to='pics')
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
health = models.CharField(max_length=120)
breedingHistory = models.TextField(max_length=300)
age = models.IntegerField()
dateAcquired = models.DateTimeField(auto_now_add=False, auto_now=True)
class Meta:
verbose_name_plural = "Species Detail"
ordering = ('species', )
def __str__(self):
return str(self.mutation)
class Pair(models.Model):
species = models.ForeignKey(Species, on_delete=models.CASCADE)
male_mutation = models.ForeignKey(Mutation, on_delete=models.CASCADE, related_name='male_mutation')
male = models.ForeignKey(SpeciesDetail, on_delete=models.CASCADE, related_name='male')
female_mutation = models.ForeignKey(Mutation, on_delete=models.CASCADE, related_name='female_mutation')
female = models.ForeignKey(SpeciesDetail, on_delete=models.CASCADE, related_name='female')
boxNo = models.IntegerField()
cageNo = models.IntegerField()
dateTimeStamp = models.DateTimeField(auto_now_add=True, auto_now=False)
description = models.TextField(max_length=300)
class Meta:
verbose_name_plural = "Pairs"
def __str__(self):
return str(self.male) + ',' + str(self.female)
views.py
def pairing(request):
if request.method == "POST":
form = Pair_ModelForm(request.POST or None)
if form.is_valid():
try:
form.save()
return redirect('breedingRecApp:pairs')
except:
pass
else:
form = Pair_ModelForm()
context = {'title': 'Pairing',
'active': 'active',
'model': Pair,
'form': form,
}
return render(request, 'breedingRecApp/pairing.html', context)
def load_mutations(request):
species_id = request.GET.get('species')
specific_mutations = Mutation.objects.filter(species_id=species_id).order_by('name')
return render(request, 'breedingRecApp/mutations_dropdown_list_options.html', {'mutations': specific_mutations})
def load_males(request):
mutation_id = request.GET.get('mutation')
print(mutation_id)
males = SpeciesDetail.objects.filter(mutation_id=mutation_id).filter(gender='M').order_by('id')
print('Male Detail', males)
return render(request, 'breedingRecApp/males_dropdown_list_options.html', {'males': males})
def load_females(request):
mutation_id = request.GET.get('mutation')
print(mutation_id)
females = SpeciesDetail.objects.filter(mutation_id=mutation_id).filter(gender='F').order_by('id')
print(females)
return render(request, 'breedingRecApp/females_dropdown_list_options.html', {'females': females})