用于状态检查的串行器

发布于 2025-01-10 13:22:10 字数 1231 浏览 0 评论 0原文

我有一个 Django 项目,其中有一家餐厅,有订单和桌子。您可以在模型下方看到:

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)

如果表仍在进行中,我应该限制创建新订单。我想这应该通过序列化器完成,但不知道验证以及如何将请求与数据库信息连接

当前序列化器(标准):

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')

I have a Django project where a restaurant with orders and tables. You can see below the model:

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)

I should restrict creating a new order if the table is still in progress. I guess it should be done through serializer but hove no idea about the validation and how to connect request with database information

current serializer (standard):

class OrdersModelSerializer(ModelSerializer):
    class Meta:
        model = Order
        fields = "__all__"

current view:

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')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恍梦境° 2025-01-17 13:22:10

您可以使用 PrimaryKeyRelatedField 指定排除具有 OrderTablequeryset=...代码>状态='in_progress':

from rest_framework import serializers

class OrdersModelSerializer(ModelSerializer):
    table = serializers.PrimaryKeyRelatedField(
        queryset=Table.objects.exclude(order_table__status='in_progress')
    )
    
    class Meta:
        model = Order
        fields = '__all__'

注意:通常最好使用 settings.AUTH_USER_MODEL [Django-doc] 来引用用户模型,而不是使用 用户直接建模[Django-doc]。有关更多信息,您可以参阅引用文档的User模型部分


You can specify the queryset=… of the PrimaryKeyRelatedField that excludes Tables for which there is an Order with status='in_progress':

from rest_framework import serializers

class OrdersModelSerializer(ModelSerializer):
    table = serializers.PrimaryKeyRelatedField(
        queryset=Table.objects.exclude(order_table__status='in_progress')
    )
    
    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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文