如何在Django REST Framework API的OneToOne序列化程序字段上允许空白字段?

时间:2019-05-12 01:21:15

标签: django django-rest-framework

发布到我的API端点时,在OneToOne字段上收到field is required错误,但是我在发布模型的save()方法中创建了OneToOne字段。

我尝试设置default=Nonenull=Trueblank=True

设备型号

class Device(models.Model):

    name = models.CharField(max_length=50)
    created = models.DateTimeField(auto_now_add=True, editable=False)
    last_updated = models.DateTimeField(auto_now=True, editable=False)
    brand = models.TextField(max_length=50)
    year_purchased = models.IntegerField()
    serial_number = models.TextField(max_length=100)
    info = models.TextField(max_length=100)
    qrcode = models.ImageField(upload_to='', blank=True, null=True)

    def save(self, **kwargs):
        super(Device, self).save(**kwargs)
        if not self.qrcode:
            self.generate_qrcode()
        if not self.checkouts.exists():
            checkout = Checkout(item=self)
            checkout.save()

结帐模型

class Checkout(models.Model):

    status_choices = (...)

    # Fields
    slug = extension_fields.AutoSlugField(populate_from='item', blank=True)
    created = models.DateTimeField(auto_now_add=True, editable=False)
    last_updated = models.DateTimeField(auto_now=True, editable=False)
    due_back = models.DateField(null=True, blank=True)
    checked_out = models.DateTimeField(blank=True, null=True)
    status = models.CharField(max_length=2, choices=status_choices, default=in_stock)

    # Relationship Fields
    user = models.OneToOneField(
        DeviceUser,
        on_delete=models.CASCADE,
        related_name="checkouts",
        blank=True,
        null=True,
        default=None,
    )

    item = models.OneToOneField(
        Device,
        on_delete=models.CASCADE,
        related_name="checkouts",
        primary_key=True,
        blank=True,
        default=None,
    )

设备序列化器

class DeviceSerializer(serializers.ModelSerializer):

    class Meta:
        model = models.Device
        fields = (
            'pk',
            'name',
            'created',
            'last_updated',
            'brand',
            'year_purchased',
            'serial_number',
            'info',
            'checkouts',
        )

发布http POST http://localhost:8000/main/api/device/ brand=test2 info=123213123 name=test2 serial_number=12321321 year_purchased=12 'Authorization: Token .....'

我希望收到确认已创建设备的确认,而不是

"checkouts": [ "This field is required." ]

1 个答案:

答案 0 :(得分:1)

checkouts 字段指定为 DeviceSerializer 序列化器,

checkouts = serializers.DateTimeField(default=None, source='checkouts.checked_out', read_only=True)


# code sample
class DeviceSerializer(serializers.ModelSerializer):
    checkouts = serializers.DateTimeField(default=None, source='checkouts.checked_out', read_only=True)

    class Meta:
        model = models.Device
        fields = (
            'pk',
            'name',
            'created',
            'last_updated',
            'brand',
            'year_purchased',
            'serial_number',
            'info',
            'checkouts',
        )