Home > database >  One to Many field in Model of Django
One to Many field in Model of Django

Time:06-14

I am creating a model in Django, but quite confuse about the type of field should i take:

Problem: I have to access multiple domains in one web classification, advice me please how do i make these field relationship so that if I try to get one web classification details then it will also contain list of related domain in that as well.

Model.py:

class WebClassification(models.Model):
    vendor_id = models.ForeignKey(Vendor, on_delete=models.CASCADE, default=None, null=True)
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=100)
    description = models.CharField(max_length=1000)
    domain_name = models.[what type i take here]

def __str__(self):
    return self.name

Domain.py

class Domain(models.Model):
    id = models.IntegerField()
    domain_name = models.CharField(max_length=200)
    
    def __str__(self):
        return self.domain_name

CodePudding user response:

If each web classification should include a list of related domain then you should include a ForeignKey field in the Domain model to WebClassification:

# models.py
class Domain(models.Model):
    id = models.IntegerField()
    web_classification = models.ForeignKey(WebClassification, on_delete=models.CASCADE)
    domain_name = models.CharField(max_length=200)
    
    def __str__(self):
        return self.domain_name

And you'd create a serializer for WebClassification model:

# serializers.py
from rest_framework import serializers

from .models import Domain, WebClassification


class WebClassificationSerializer(serializers.ModelSerializer):
    domains = serializers.SerializerMethodField('get_domains')

    def get_domains(self, id):
        return Domain.objects.filter(web_classification=id).values()

    class Meta:
        model = WebClassification
        fields = "__all__"

And use WebClassificationSerializer in a view:

# views.py

from rest_framework import generics

from .serializers import WebClassificationSerializer
from .models import WebClassification

class WebClassificationListAPIView(generics.ListAPIView):
    serializer_class = WebClassificationSerializer
    queryset = WebClassification.objects.all()

CodePudding user response:

You can do it in two ways. It depends if one Domain may have multiple WebClassification or just one:

# one Domain may have multiple WebClassifications

class WebClassification(models.Model):
    ...
    domains = models.ManyToManyField("Domain")

# or

# one Domain may have one WebClassification

class Domain(models.Model):
    ...
    web_classification = ForeignKey("WebClassification", on_delete=models.CASCADE, default=None, null=True, related_name="domains")

with both methods you can access all domains related for one WebClassification with:

web_classification = WebClassification.objects.create(...)
web_classification.domains.all()

and in template

{{ web_classification.domains.all }}
  • Related