2

I have 2 classes EligibilityCriteria, Scholarship. Scholarship references EligibilityCriteria as a foreign key.

class EligibilityCriteria(models.Model):
    criteria_id = models.AutoField(primary_key=True)
    gender = models.ForeignKey(Gender, null=True, blank=True)
    minimum_age = models.IntegerField(blank=True, null=True)
    maximum_age = models.IntegerField(blank=True, null=True)
    eligibility_colleges = models.TextField(blank=True, null=True)
    state = models.ForeignKey(State, blank=True, null=True, on_delete=SET_NULL)
    caste = models.ForeignKey(Caste, blank=True, null=True, on_delete=SET_NULL)
    religion = models.ForeignKey(Religion, blank=True, null=True, on_delete=SET_NULL)
    education_level = models.ForeignKey(Education_level, blank=True, null=True, on_delete=SET_NULL)
    minimum_percentage = models.CharField(max_length=100, blank=True, null=True)
    income_ceiling = models.IntegerField(blank=True, null=True)
    other_criteria = models.TextField(blank=True, null=True)

    def __unicode__(self):
        return str(self.education_level) + "_" + str(self.state) + "_" + str(self.gender)

class Scholarship(models.Model):
    scholarship_id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=500)
    description = models.TextField()
    eligibility_criteria = models.ForeignKey(EligibilityCriteria, null=True, on_delete=models.CASCADE)
    scholarship_category = models.ForeignKey(ApplicantCategory, blank=False, null=False, on_delete=CASCADE)
    end_date = models.DateTimeField(null=True, blank=True)
    availability_count = models.IntegerField(null=True, blank=True)
    website_link = models.CharField(max_length=500, blank=True, null=True)
    amount = models.CharField(max_length=100, blank=True, null=True)
    selection_procedure = models.TextField(blank=True, null=True)

    def __unicode__(self):
        return str(self.name)

Now when I convert QuerySet of Scholarships to json, the values of EligibilityCriteria are missing:

def get_all_scholarships(request):
    scholarships_list = Scholarship.objects.all()
    # return JsonResponse(scholarships_list, safe=False)
    response_obj = serializers.serialize('json', scholarships_list,
                                 fields=('name','description', 'eligibility_criteria', 'scholarship_category', 'end_date',
                                         'availability_count', 'website_link', 'amount', 'selection_procedure'))

    return django.http.HttpResponse(__set.encode('utf-8'), content_type='application/json')

Result:

[{"model": "scholarship.scholarship", "pk": 1, "fields": {"name": "BlynQ Creative Scholarship", "description": "Most creative UI Designer", "eligibility_criteria": 2, "scholarship_category": 5, "end_date": "2017-02-20T00:00:00Z", "availability_count": null, "website_link": "www.blynq.in", "amount": "20000", "selection_procedure": "From open source repositories"}]

How do I convert QuerySet of Scholarship objects to json in such a way that even values within eligibility criteria are visible?

2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.