在django rest框架项目中,我有以下模型。在这个项目中有产品,他们有生产者和进口商。
# models.py
class Product(models.Model):
name = models.CharField(max_length=255)
producer = models.ManyToManyField(Company, related_name='producer')
importer = models.ManyToManyField(Company, related_name='importer')
class C
在Django REST框架(2.1.16)中,我有一个带有可为空的FK字段type的模型,但是POST创建请求给出了400 bad request,它表示需要该字段。
我的模型是
class Product(Model):
barcode = models.CharField(max_length=13)
type = models.ForeignKey(ProdType, null=True, blank=True)
序列化程序是:
class ProductSerializer(serializers.ModelSerializer):
class Meta:
我有一个具有多个ForeignKey关系的模型,并希望构建一个POST API,它允许我发布详细的名称,而不是id的名称。这似乎是SlugRelatedField的一项工作,但是这不允许我提供一个基于JSON中其他字段过滤的查询集。问题是,需要根据JSON请求中的其他字段对字段进行筛选。
class GenericReportSerializer(serializers.ModelSerializer):
"""
GenericReportSerializer for POST.
"""
organization
serializers.py:
from django.forms import widgets
from rest_framework import serializers
from .models import Classname, Sectionname, Teachername, Attendancename
from django.contrib.auth.models import User
class UserSerializer(serializers.HyperlinkedModelSerializer):
standard = serializers.Prima
我对Django的REST框架有疑问。
当产品呈现到远程客户端时,每个产品都会使用过滤后的数据进行字段处理。
例如,模型可能是这样的。
class Product(models.Model):
name = models.CharField()
class Like(models.Model):
product = models.ForeignKey(Product, related_name="likes")
在客户端上,每个都喜欢用真值计数的产品的,而不是假的。
所以我在序列化程序中尝试了下面的代码。
class ProductSerializ
我有一个User模型和一个Present模型。一个User可以有多个Present,但是Present有一个唯一的User
我的models.py是:
from django.db import models
from django.contrib.auth.models import User
class Present(models.Model):
name = models.CharField(max_length=15)
price = models.FloatField()
link = models.CharField(max_length=15)
我有个模特
class Like(models.Model):
product = models.ForeignKey('ProductInStore', on_delete=models.PROTECT)
user = models.ForeignKey('users.CustomUser', on_delete=models.PROTECT)
class Meta:
unique_together = ('product', 'user',)
和串行化器
class LikeSeria