form is not shown despite being nested to company model

Viewed 75

The use case of the app I am trying to develop is about registering the company who are interested for franchise. I have taken this concept from franchisebazar for learning purpose. There i saw company info, brand info and business model for registering the company. That is why I created the following model. I want the create and update view now so from the frontend i can hit the api request to create and update. I designed the following serializer and APIView. However, when i try to test in the browser, if the coded way is working or not, i could not see the form for brand and business model despite they are nested in company. Thus, I want to know if i have to extend it more by creating a view for brand and business model separately or i did something wrong in my code.

class Company(models.Model):
    owner = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    name = models.CharField(max_length=150, blank=False, null=False)
    phone_number = models.CharField(max_length=15, blank=False, null=False)


class Brand(models.Model):

    company = models.ForeignKey(Company, related_name='company_brand', on_delete=models.CASCADE)
    name = models.CharField(max_length=150, blank=False, null=False)
    website = models.URLField()
    description = models.TextField(blank=False)


class BusinessModel(models.Model):


    company = models.ForeignKey(Company, related_name='company_business_model', on_delete=models.CASCADE)
    industry = models.ForeignKey(Industry, null=True, related_name='industry', on_delete=models.SET_NULL)
    segments = models.ForeignKey(Segment, on_delete=models.SET_NULL, null=True)
    total_investment = models.CharField(max_length=50, choices=TOTAL_INVESTMENT, default=None)

    def __str__(self):
        return self.company.name


class CompanyView(views.APIView):

    serializer_class = CompanySerializer

    def get(self, request, pk=None, format=None):
        reply = {}
        try:
            companies = Company.objects.all()
            if pk:
                company = companies.get(pk=pk)
                reply['data'] = self.serializer_class(company).data
            reply['data'] = self.serializer_class(companies, many=True).data
        except Company.DoesNotExist:
            reply['data'] = []
        return Response(reply, status.HTTP_200_OK)


    def post(self, request, pk=None, format=None):
        reply = {}
        company={}
        if pk is not None:
            try:
                company = Company.object.get(pk=pk)
            except Company.DoesNotExist:
                return error.RequestedResourceNotFound().as_response()
        serialized_data = self.serializer_class(instance=company, data=request.data, partial=True)
        if serialized_data.is_valid():
            serialized_data.save(owner=request.user)
        else:
            return error.ValidationError(serialized_data.errors).as_response()
        reply['data'] = serialized_data.data
        return Response(reply, status.HTTP_200_OK)


class CompanySerializer(serializers.ModelSerializer):

    company_brand = BrandSerializer(many=True)
    company_business_model = BusinessModelSerializer(many=True)

    class Meta:

        model = Company
        fields = '__all__'
1 Answers

One reason why you don't see the form which allows you update brand and business model instances is because the default behaviour of model serializer doesn't allow you to do that. You need to write your own custom update method in your serializer to save the changes made to the brand and business instances.

Related