问题描述
我遇到了这个错误,我已经搜索并解决了这个问题,并且已经添加了date_time变量,但是不能,我只想使用viewset,请帮我解决:(
I got this err, i already searched and get this resolved and already add date_time variable but cant and I just want to use viewset instead, please help me solve :(
django.core.exceptions.FieldError: Cannot resolve keyword 'transaction' into field. Choices are: address, created_at, deleted_at, id, notification, phone_number, status, total, transactionvariant, updated_at, user, user_id
这是我的def destroy(views.py):
here is my def destroy(views.py):
class TransactionView(viewsets.ViewSet):
def list(self, request):
user = get_object_or_404(User, pk=request.user.id)
queryset = Transaction.objects.filter(user_id=user).exclude(deleted_at__isnull=False)
serializer = TransactionSerializer(queryset, many=True)
return Response(serializer.data, status=200)
def retrieve(self, request, pk=None):
user = get_object_or_404(User, pk=request.user.id)
transaction = TransactionVariant.objects.filter(transaction__pk=pk).filter(transaction__user_id=user)
serializer = TransactionVariantSerializer(transaction, many=True)
return Response(serializer.data)
def destroy(self, request, pk=None):
date_time = datetime.now()
# user = get_object_or_404(User, pk=request.user.id)
transaction = Transaction.objects.filter(transaction__pk=pk).update(deleted_at=date_time)
serializer = TransactionSerializer(transaction, many=True)
return Response(serializer.data)
和我的serializers.py
and my serializers.py
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = ('name',)
class VariantSerializer(serializers.ModelSerializer):
# product = ProductSerializer(many=False, read_only=True)
class Meta:
model = Variant
fields = ('name', 'size', 'color', 'price', 'image_link',)
class TransactionVariantSerializer(serializers.ModelSerializer):
variant = VariantSerializer(many=False, read_only=True)
# products = serializers.VariantSerializer(variant)
# products = serializers.(fields='variant')
class Meta:
model = TransactionVariant
fields = ('quantity','variant')
class TransactionSerializer(serializers.ModelSerializer):
transaction_id = serializers.IntegerField(source='id')
# product = ProductSerializer(many=False, read_only=True)
# variant = VariantSerializer(many=False, read_only=True)
class Meta:
model = Transaction
# fields = ('transaction_id','user_id','status','total','created_at')
fields = '__all__'
推荐答案
我已经解决了!这是我的新代码:
I already fix it! and here is my new code:
def destroy(self, request, pk=None):
# transaction_id = TransactionVariant.objects.filter(transaction__pk=transactionvariant.transaction__pk)
# transaction_variant = TransactionVariant.objects.select_related('transaction').get(id=1)
# print("hihihhihiihihi",transaction_variant)
date_time = datetime.now()
user = get_object_or_404(User, pk=request.user.id)
transaction = TransactionVariant.objects.filter(transaction__pk=pk).update(deleted_at=date_time)
transaction = Transaction.objects.filter(id=pk).update(deleted_at=date_time)
print("adsfasdf", transaction)
serializer = TransactionSerializer(transaction, many=True)
return Response(serializer.data, many=True)
但是之后,使用url时出现了新错误( http://localhost:8000/api/transactions/3/)方法删除,我的数据库当然已经更新了transaction和transaction_variant的Deleted字段,但显示以下日志:TypeError:"int"对象不可迭代
but after that i got new error when using url (http://localhost:8000/api/transactions/3/) METHOD delete, my database of course has update the deleted field for both transaction and transaction_variant but show this log :TypeError: 'int' object is not iterable
您可以从顶部引用我的serializer.py
you can reference my serializer.py from the top
这篇关于无法使用def destroy viewset.viewset(软删除)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!