我有两个模型 Requests
和Events
,其外键返回User
模型。当尝试使用Django Rest Framework发布外键数据时,我收到 null约束错误。似乎django不接受 user_id (requester_id
和event_admin_id
)引用。在可浏览的API中,外键部分(requester_id
和event_id
)不存在。它被某些东西隐藏了吗?请注意,event_participant仅由外键组成,但在发布时会显示在可浏览的API中。我不确定会有什么区别
Missing event_admin_id foreignkey field Missing request_admin_id fk field
models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
class User(AbstractUser):
user_job_description = models.CharField(max_length = 200)
user_profile_pic = models.ImageField(null = True, blank = True)
class Event(models.Model):
def __str__(self):
return self.event_name
event_name = models.CharField(max_length = 100)
event_location = models.CharField(max_length = 100)
event_description = models.TextField(max_length = 300)
admin_user_id = models.ForeignKey(User, related_name = 'event_admin')
class Request(models.Model):
def __str__(self):
return self.request_desc
requester_user_id = models.ForeignKey(User, related_name = 'requests')
request_desc = models.TextField(max_length = 100)
fulfiller_user_id = models.IntegerField(null = True, blank = True)
fulfilled_status = models.BooleanField()
class Event_Participant(models.Model):
def __str__(self):
return str(self.event_id)
user_id = models.ForeignKey(User, related_name = 'event_attendee')
event_id = models.ForeignKey(Event, related_name = 'event')
serializers.py
from django.contrib.auth import get_user_model
from crewboardsdb.models import Event
from crewboardsdb.models import Request
from crewboardsdb.models import Event_Participant
from rest_framework import serializers
class UserSerializer(serializers.ModelSerializer):
requests = serializers.StringRelatedField(many = True)
event_admin = serializers.StringRelatedField(many = True)
event_attendee= serializers.StringRelatedField(many = True)
class Meta:
model = get_user_model()
fields = ('id','username','first_name','last_name','email','user_job_description','event_admin','requests', 'event_attendee',)
class EventSerializer(serializers.ModelSerializer):
admin_user_id = serializers.StringRelatedField()
class Meta:
model = Event
fields = ('id', 'event_name', 'admin_user_id', 'event_description', 'event_location',)
lookup_field = 'admin_user_id'
class RequestSerializer(serializers.ModelSerializer):
requester_user_id = serializers.StringRelatedField()
class Meta:
model = Request
fields = ('id', 'request_desc', 'fulfilled_status','requester_user_id','fulfiller_user_id',)
lookup_field = 'requester_user_id'
class EventParticipantSerializer(serializers.ModelSerializer):
class Meta:
model = Event_Participant
fields = ('id', 'event_id', 'user_id')
答案 0 :(得分:1)
希望这仍然是活跃的,刚刚遇到同样的问题并想出来。
在我的情况下,我也有完全相同的问题,外键关联的属性没有显示在可浏览的api中,所以我认为它必须是序列化器的东西。
原来这就是问题所在。如果您有ForeignKey
约束,则根本不需要处理nestes序列化程序,只需将它们从序列化程序类中删除即可!
在你的情况下,它会是这样的:
(Event Model)
class Event(models.Model):
def __str__(self):
return self.event_name
event_name = models.CharField(max_length = 100)
event_location = models.CharField(max_length = 100)
event_description = models.TextField(max_length = 300)
admin_user_id = models.ForeignKey(User, related_name = 'event_admin')
此处admin_user_id
可能是您的可浏览API中未显示的属性。
现在,这是该类的序列化程序必须如下所示:
class EventSerializer(serializers.ModelSerializer):
class Meta:
model = Event
fields = ('id', 'event_name', 'admin_user_id', 'event_description', 'event_location',)
lookup_field = 'admin_user_id'
请注意,只需删除行admin_user_id = serializers.StringRelatedField()
即可完成作业,现在您的api应按预期显示该字段,并且您可以选择将该对象链接到的用户。
我在自己的项目中测试了你的问题,只需要你的模型和序列化器,按照这个简单的步骤完成工作!
希望这对您或将来的任何人都有帮助。欢呼声。