AssertionError в проекте DjangoPython

Программы на Python
Anonymous
AssertionError в проекте Django

Сообщение Anonymous »

Я пытаюсь протестировать API для создания компании, но выдает ошибку утверждения. В нем говорится, что мне нужно создать собственный метод .create(), но я уже определил это, но все еще получаю сообщение об ошибке.
serializer.py

Код: Выделить всё

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
И ошибка:

Код: Выделить всё

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.
Я уже определил собственный метод создания, но все еще получаю сообщение об ошибке.

Подробнее здесь: https://stackoverflow.com/questions/786 ... go-project

Вернуться в «Python»