OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

AssertionError in django project

  • Thread starter Thread starter Subhajit Das
  • Start date Start date
S

Subhajit Das

Guest
I am trying to test a API to create a company but it gives me a assertion error, it says that i need to create a custom .create() method but I have already defined that but still getting the error.

serializer.py

```python

Code:
class TaxInformationSerializer(serializers.ModelSerializer):
    class Meta:
        model = TaxInformation
        fields = '__all__'
    def create(self, validated_data):
        return TaxInformation.objects.create(**validated_data)

class CompanySerializer(serializers.ModelSerializer):
    tax_information = TaxInformationSerializer()



    class Meta:
        model = Company
        fields = ["brand_name", "legal_name", "mobile", "telephone_no", "fax", "status", "tax_information"]
        read_only_fields = ['user', 'created_at']

    def create(self, validated_data):
        # Automatically set the user to the request user
        request = self.context.get('request')
        if request and hasattr(request, 'user'):
            validated_data['user'] = request.user
        return super().create(validated_data)

        # Create the tax information instance
        tax_information_data = validated_data.pop('tax_information')
        # tax_information = TaxInformation.objects.create(**tax_information_data)
        tax_information_serializer = TaxInformationSerializer(data=tax_information_data)
        tax_information_serializer.is_valid(raise_exception=True)
        tax_information = tax_information_serializer.save()

        # Create the company instance with the tax information
        instance = Company.objects.create(tax_information=tax_information, **validated_data)
        return instance

and the error is

Code:
AssertionError at /datamanagement/company
The `.create()` method does not support writable nested fields by default.
Write an explicit `.create()` method for serializer `datamanagement.serializers.CompanySerializer`, or set `read_only=True` on nested serializer fields.

 I have already defined a custom create method but still getting the error.
<p>I am trying to test a API to create a company but it gives me a assertion error, it says that i need to create a custom .create() method but I have already defined that but still getting the error.</p>
<p>serializer.py</p>
<p>```python</p>
<pre><code>class TaxInformationSerializer(serializers.ModelSerializer):
class Meta:
model = TaxInformation
fields = '__all__'
def create(self, validated_data):
return TaxInformation.objects.create(**validated_data)

class CompanySerializer(serializers.ModelSerializer):
tax_information = TaxInformationSerializer()



class Meta:
model = Company
fields = ["brand_name", "legal_name", "mobile", "telephone_no", "fax", "status", "tax_information"]
read_only_fields = ['user', 'created_at']

def create(self, validated_data):
# Automatically set the user to the request user
request = self.context.get('request')
if request and hasattr(request, 'user'):
validated_data['user'] = request.user
return super().create(validated_data)

# Create the tax information instance
tax_information_data = validated_data.pop('tax_information')
# tax_information = TaxInformation.objects.create(**tax_information_data)
tax_information_serializer = TaxInformationSerializer(data=tax_information_data)
tax_information_serializer.is_valid(raise_exception=True)
tax_information = tax_information_serializer.save()

# Create the company instance with the tax information
instance = Company.objects.create(tax_information=tax_information, **validated_data)
return instance
</code></pre>
<p>and the error is</p>
<pre><code>AssertionError at /datamanagement/company
The `.create()` method does not support writable nested fields by default.
Write an explicit `.create()` method for serializer `datamanagement.serializers.CompanySerializer`, or set `read_only=True` on nested serializer fields.

I have already defined a custom create method but still getting the error.
</code></pre>
 

Latest posts

Top