Sunday, December 29, 2019

Django ValueError at: Cannot assign "": "Pair.male_mutation" must be a "Mutation" instance."

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:
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):

    def get_absolute_url_delete(self):

        return reverse("breedingRecApp:delete_mutation", kwargs={"mutation_id":})

class SpeciesDetail(models.Model):

        ('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)

def pairing(request):
    if request.method == "POST":
        form = Pair_ModelForm(request.POST or None)
        if form.is_valid():
                return redirect('breedingRecApp:pairs')
        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')
    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')
    females = SpeciesDetail.objects.filter(mutation_id=mutation_id).filter(gender='F').order_by('id')
    return render(request, 'breedingRecApp/females_dropdown_list_options.html', {'females': females})

You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit

No comments:

Post a Comment