Home > Software design >  Need to add one model fields in the another model serializers but throwing error while POST the requ
Need to add one model fields in the another model serializers but throwing error while POST the requ

Time:04-26

models.py

class Product(models.Model):
    product_id = models.AutoField(unique=True, primary_key=True)
    product_name = models.CharField(max_length=255)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = "product_master"

    def __str__(self):
        return self.product_name

class Organisation(models.Model):
    """
    Organisation model
    """
    org_id = models.AutoField(unique=True, primary_key=True)
    org_name = models.CharField(max_length=100)
    org_code = models.CharField(max_length=20)
    org_mail_id = models.EmailField(max_length=100)
    org_phone_number = models.CharField(max_length=20)
    org_address = models.JSONField(max_length=500, null=True)
    product = models.ManyToManyField(Product, related_name='products')
    org_logo = models.ImageField(upload_to='org_logo/')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = "organisation_master"

    def __str__(self):
        return self.org_name

serializers.py

class Product_Serializers(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = ('product_id', 'product_name',)

class Organisation_Serializers(serializers.ModelSerializer):
    product = Product_Serializers(many=True)
    class Meta:
        model = Organisation
        fields = ('org_id', 'org_name', 'org_address', 'org_phone_number', 'org_mail_id','org_logo','org_code','product')
        depth = 1

" While i tried to do POST method for the organisation model I have tried giving the input for product as "product: 5" and "product: {"product_id": 5,"product_name": "time"} in the postman form data but it is showing as

{
    "status": "error",
    "code": 400,
    "data": {
        "product": [
            "This field is required."
        ]
    },
    "message": "success"
}

Views.py

class Organisation_Viewset(DestroyWithPayloadMixin,viewsets.ModelViewSet):
    renderer_classes = (CustomRenderer, )  #ModelViewSet Provides the list, create, retrieve, update, destroy actions.
    queryset=models.Organisation.objects.all()
    parser_classes = [MultiPartParser, FormParser]
    serializer_class=serializers.Organisation_Serializers


    def create(self, request, *args, **kwargs):
        data = request.data
        new_organisation= models.Organisation.objects.create(org_name=data["org_name"],org_code = ["org_code"], org_mail_id =data["org_mail_id"],org_phone_number= data["org_phone_number"], org_address=data["org_address"],org_logo = data["org_logo"])

        new_organisation.save()

        for product in data["product"]:
            product_id = models.Product.objects.get(product_id=product["product_id"])
            new_organisation.products.add(product_id)

        serializer = serializers.Organisation_serializers(new_organisation)

        return Response(serializer.data)

I need to post like this product: {"product_id": 5,"product_name": "time"}, what fields are available in the product model it should be posted on this product field.

Can you please suggest me a way as i tried many ways as per my knowledge but it dosen't worked.

CodePudding user response:

you are using a tuple for fields, put a comma behind product in youre fields. If you use a list then dont use an end comma

fields = ('org_id', 'org_name', 'org_address', 'org_phone_number', 'org_mail_id','org_logo','org_code','product',)
        depth = 1

CodePudding user response:

Update your serializers to:

class Organisation_Serializers(serializers.ModelSerializer):
    class Meta:
        model = Organisation
        fields = ('org_id','org_name','org_address','org_phone_number','org_mail_id','org_logo','org_code','product',)

Update your views as:

class Organisation_Viewset(DestroyWithPayloadMixin,viewsets.ModelViewSet):
    renderer_classes = (CustomRenderer, )  #ModelViewSet Provides the list, create, retrieve, update, destroy actions.
    queryset=models.Organisation.objects.all()
    parser_classes = [MultiPartParser, FormParser]
    serializer_class=serializers.Organisation_Serializers


    def create(self, request, *args, **kwargs):
        data = request.data
        serializer = Organisation_Serializers(data)
        serializer.is_valid(raise_exceptions=True)
        serializer.save()
        return Response(serializer.data)

Now we can send the id as an array for the create api:

curl -XPOST \
     -H [authentication]
     http://localhost:8000/some/api/url
     -d '{"org_name": "hero", "product": [1,2]}'
  • Related