用于状态检查的序列化程序
Serializer for status check
我有一个 Django 项目,其中一家餐厅有订单和 tables。您可以在下面看到模型:
class Order(models.Model):
STATUS_CHOICES = (
("in_progress", "In_progress"),
('completed', 'Completed')
)
table = models.ForeignKey(Table, on_delete=models.CASCADE, blank=False, null=False, related_name='order_table')
user = models.ForeignKey(User, on_delete=models.CASCADE, blank=False, null=False, related_name='order_user')
status = models.CharField(choices=STATUS_CHOICES, null=True, max_length=15)
如果 table 仍在进行中,我应该限制创建新订单。我想它应该通过序列化程序完成,但不知道验证以及如何将请求与数据库信息连接
当前序列化程序(标准):
class OrdersModelSerializer(ModelSerializer):
class Meta:
model = Order
fields = "__all__"
当前视图:
class OrdersFilter(filters.FilterSet):
class Meta:
model = Order
fields = (
'user', 'table__id', 'status',
)
class OrdersModelViewSet(ModelViewSet):
queryset = Order.objects.all()
serializer_class = OrdersModelSerializer
pagination_class = LimitOffsetPagination
filter_backends = (DjangoFilterBackend, OrderingFilter)
filter_class = OrdersFilter
ordering_fields = ('table', 'user', 'status')
您可以指定 PrimaryKeyRelatedField
中的 queryset=…
排除 Table
,因为 Order
和 status='in_progress'
:
from rest_framework import serializers
class OrdersModelSerializer(ModelSerializer):
table = serializers.PrimaryKeyRelatedField(
<strong>queryset=Table.objects.exclude(order_table__status='in_progress')</strong>
)
class Meta:
model = Order
fields = '__all__'
Note: It is normally better to make use of the settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use the User
model [Django-doc] directly. For more information you can see the referencing the User
model section of the documentation.
我有一个 Django 项目,其中一家餐厅有订单和 tables。您可以在下面看到模型:
class Order(models.Model):
STATUS_CHOICES = (
("in_progress", "In_progress"),
('completed', 'Completed')
)
table = models.ForeignKey(Table, on_delete=models.CASCADE, blank=False, null=False, related_name='order_table')
user = models.ForeignKey(User, on_delete=models.CASCADE, blank=False, null=False, related_name='order_user')
status = models.CharField(choices=STATUS_CHOICES, null=True, max_length=15)
如果 table 仍在进行中,我应该限制创建新订单。我想它应该通过序列化程序完成,但不知道验证以及如何将请求与数据库信息连接
当前序列化程序(标准):
class OrdersModelSerializer(ModelSerializer):
class Meta:
model = Order
fields = "__all__"
当前视图:
class OrdersFilter(filters.FilterSet):
class Meta:
model = Order
fields = (
'user', 'table__id', 'status',
)
class OrdersModelViewSet(ModelViewSet):
queryset = Order.objects.all()
serializer_class = OrdersModelSerializer
pagination_class = LimitOffsetPagination
filter_backends = (DjangoFilterBackend, OrderingFilter)
filter_class = OrdersFilter
ordering_fields = ('table', 'user', 'status')
您可以指定 PrimaryKeyRelatedField
中的 queryset=…
排除 Table
,因为 Order
和 status='in_progress'
:
from rest_framework import serializers
class OrdersModelSerializer(ModelSerializer):
table = serializers.PrimaryKeyRelatedField(
<strong>queryset=Table.objects.exclude(order_table__status='in_progress')</strong>
)
class Meta:
model = Order
fields = '__all__'
Note: It is normally better to make use of the
settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use theUser
model [Django-doc] directly. For more information you can see the referencing theUser
model section of the documentation.