我正在尝试使用用户的officeid保存项目,但它向我抛出错误
值错误:无法将“〈CustomUser:admin@gmail.com":“ClearanceItem.office”必须是“Office”示例。
我的customuser的id为1
而电子邮件是admin@gmail.com
最后是office id = 'OSA'
这是我的models.py
class CustomUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(_('email address'), unique=True)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
date_joined = models.DateTimeField(default=timezone.now)
userid = models.CharField(null=True, max_length=9)
officeid = models.ForeignKey('Office', models.DO_NOTHING, db_column='officeid', blank=True, null=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = CustomUserManager()
class ClearanceItem(models.Model):
cl_itemid = models.CharField(primary_key=True, max_length=20, default=get_default_id)
studid = models.CharField(max_length=9, blank=True, null=True)
office = models.ForeignKey('Office', models.DO_NOTHING, blank=True, null=True)
sem = models.CharField(max_length=1, blank=True, null=True)
sy = models.CharField(max_length=9, blank=True, null=True)
remarks = models.TextField(blank=True, null=True)
resolution = models.TextField(blank=True, null=True)
resolve = models.BooleanField(default='False', blank=True, null=True)
resolve_date = models.DateField(blank=True, null=True)
resolve_by = models.CharField(max_length=8, blank=True, null=True)
recorded_by = models.CharField(max_length=8, blank=True, null=True)
record_date = models.DateField(auto_now_add = True, blank=True, null=True)
class Meta:
managed = False
db_table = 'clearance_item'
class Office(models.Model):
office_id = models.CharField(primary_key=True, max_length=50)
office_name = models.CharField(max_length=200)
office_head = models.CharField(max_length=8, blank=True, null=True)
designation = models.TextField(blank=True, null=True)
office_parent = models.CharField(max_length=50, blank=True, null=True)
deptlogo = models.TextField(blank=True, null=True)
class Meta:
managed = False
db_table = 'office'
这是我的views.py
class APIClerkView(generics.ListCreateAPIView):
permission_classes = [IsAuthenticated]
serializer_class = ClearanceItemSerialize
def perform_create(self, serializer):
serializer.save(office=CustomUser.objects.get(officeid=self.request.user.officeid),
recorded_by=self.request.user.userid)
根据我以前遇到的这个问题,我认为它不是抛出一个实际的officeid,而是抛出一个字符串
我该如何补救或有其他解决方案吗
我在这里的目标是避免用户在字段中输入他们的办公室ID沿着其他详细信息。
{
"sem": "1",
"sy": "2022-2023",
"remarks": "TEST",
"resolution": "TEST",
"studid": "2012-5037"
}
1条答案
按热度按时间qnakjoqk1#
你可以这样做