简体   繁体   中英

api design concept using django rest framework

My question or say my confusion is

Does api design depends on how we present in UI?

I will illustrate my confusion with one of an example. Will the api design be different if i want the form to be in multiple step? I mean in the https://www.franchisebazar.com/franchisor-registration if you see the form for company registration, there are section like company information, company brand and business model which are associated with company model. The submit button is only one that means there will be only one api for posting the company data.

But what if I want the UI be different(multiple steps wizard form instead of one single big form) like company info in the first step and after submitting company personal info, the next step will be brand and then business model at last. For this do i need to design an api separately? One for company personal info, one for brand and one for business model.

up to now my design is following

https://gist.github.com/MilanRgm/132eb6c0ba0cf66e48fa0ca4c17ef732

I will brief it in the list again

1) API design for registering company profile using one single big form

2) API design for registering company profile using multiple step way

I am confused though. Any kind of help from community is highly appreciated.

Here is the code as well which i have come up with

models.py

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

serializers.py

class BrandSerializer(serializers.ModelSerializer):

    class Meta:

        model = Brand
        fields = '__all__'



class BusinessModelSerializer(serializers.ModelSerializer):

    class Meta:

        model = BusinessModel
        fields = '__all__'




class CompanySerializer(serializers.ModelSerializer):

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

    class Meta:

        model = Company
        fields = '__all__'

views.py

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)

Design of your API depends on the business case. Sometimes, it depends on GUI also. In your case, it is better to design one API. But at the front end, you can design according to your need. You can design multi-step form, save this at Javascript objects, at the end of the process, you can POST this multi-step form data to your API. Or, you can just design on a big form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM