# I want to have a link so that i can easily know that inherited class is related to this class object.

**URL:** <https://forum.djangoproject.com/t/i-want-to-have-a-link-so-that-i-can-easily-know-that-inherited-class-is-related-to-this-class-object/24404>\
**Category:** Using the ORM\
**Created:** [October 8, 2023, 2:41pm UTC](https://forum.djangoproject.com/t/i-want-to-have-a-link-so-that-i-can-easily-know-that-inherited-class-is-related-to-this-class-object/24404 "2023-10-08T14:41:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dr-shahiamrit](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dr-shahiamrit/32/16325_2.png) [@dr-shahiamrit](https://forum.djangoproject.com/u/dr-shahiamrit)\
**Post date:** [October 8, 2023, 2:41pm UTC](https://forum.djangoproject.com/t/i-want-to-have-a-link-so-that-i-can-easily-know-that-inherited-class-is-related-to-this-class-object/24404/1 "2023-10-08T14:41:54Z")

</div>

In the following SEClass has two subclass inheried using manytomany field. whenever use sent data from react it needs to selected that this instance has

```auto
    class educationHistory(models.Model):
        sn = models.CharField(max_length=100, blank=True, null=True)
        degree_name = models.CharField(max_length=100, blank=True, null=True)
        board_or_university = models.CharField(max_length=100, blank=True, null=True)
    
    class awardsMeritsScholarship(models.Model):
        sn = models.CharField(max_length=100, blank=True, null=True)
        year = models.CharField(max_length=100, blank=True, null=True)
        title = models.CharField(max_length=100, blank=True, null=True)
    
    class SEClass(models.Model):
        # Education History
        education_history = models.ManyToManyField(educationHistory, blank=True)
        # Awards, Merits and Scholarship
        awardsMeritsScholarship = models.ManyToManyField(awardsMeritsScholarship, blank=True)
    
    my views.py
    class SEClassView(APIView):
        def get(self, request):
            instances = SEClass.objects.all()
            serializer = SEClassSerializer(instances, many=True)
            return Response(serializer.data)
    
        def post(self, request, format=None):
            data = request.data
            if not isinstance(data, list):
                return Response(
                    {"error": "Data should be a list of objects."},
                    status=status.HTTP_400_BAD_REQUEST
                )
    
            save_data = []
    
            for item in data:
                education_history_data = item.pop('education_history', [])
                awards_merits_scholarship_data = item.pop('awardsMeritsScholarship', [])
    
                # Create SEClass instance
                se_class_serializer = SEClassSerializer(data=item)
                if not se_class_serializer.is_valid():
                    return Response(se_class_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
                se_class_instance = se_class_serializer.save()
    
                # Save educationHistory instances and link to SEClass
                for edu_item in education_history_data:
                    edu_item['se_class_id'] = se_class_instance.id
                    edu_serializer = educationHistorySearilizer(data=edu_item)
                    if edu_serializer.is_valid():
                        edu_serializer.save()
    
                # Save awardsMeritsScholarship instances and link to SEClass
                for awards_item in awards_merits_scholarship_data:
                    awards_item['se_class_id'] = se_class_instance.id
                    awards_serializer = awardsMeritsScholarshipSearilizer(data=awards_item)
                    if awards_serializer.is_valid():
                        awards_serializer.save()
    
                save_data.append(se_class_serializer.data)
    
            return Response(save_data, status=status.HTTP_201_CREATED)

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 8, 2023, 2:52pm UTC](https://forum.djangoproject.com/t/i-want-to-have-a-link-so-that-i-can-easily-know-that-inherited-class-is-related-to-this-class-object/24404/2 "2023-10-08T14:52:37Z")

</div>

Side note: Please don’t try to describe the entire issue in the title. The title should just be a general statement identify what you’re looking for. Use the body of the post to add the details. Also, when posting code here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted. (I’ve taken the liberty of cleaning up what I could.)

You may want to become more familiar with the proper terminology used by Python, Django, and databases. The SEClass model does **not** have two subclasses and there is no “inheritance” involved.

The SEClass is related to two other models by ManyToMany fields.

Please provide more detail about what you mean by “I want to have a link so that i can easily know that inherited class is related to this class object.”  
You may want to try and word it as "If I have an instance of “X”, how do I get the related instances of “Y”? (For whatever models “X” and “Y” may be.)

You should also probably review the docs at:

- [Many-to-many relationships | Django documentation | Django](https://docs.djangoproject.com/en/4.2/topics/db/examples/many_to_many/)
- [Related objects reference | Django documentation | Django](https://docs.djangoproject.com/en/4.2/ref/models/relations/)  
and
- [Making queries | Django documentation | Django](https://docs.djangoproject.com/en/4.2/topics/db/queries/#lookups-that-span-relationships)
