following are the codes
in model.py
class Channel(models.Model):# id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) id = models.AutoField(primary_key=True) name = models.CharField(max_length=100, default='') language = models.ForeignKey(Language, on_delete=models.CASCADE,default='en') parent = models.ForeignKey("self", related_name='children',default=0,blank=True,null=True,on_delete=models.CASCADE) image = models.ForeignKey(Image, related_name='channel_image', on_delete=models.CASCADE,default=0) display_index = models.IntegerField(default=0) release = models.BooleanField(default=False) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) def _str_(self) -> str: return self.name
in Scehma.py
class ChannelSchema(ModelSchema):
class Config:
model = Channel
model_fields = [“id”,“name”, “language”, “parent”,“image”,“display_index”,“release”,“created_at”,“updated_at”]in api.py
@api.post(“/channels/”)
def channels(request,data:ChannelSchema):
qs = Channel.objects.create(**data.dict()) return {"post channels":"completed"+qs.name}
when I post data to /channels
I get first error or second error.
first error
ValueError: Cannot assign “0”: “Channel.language” must be a “Language” instance.
POST obj
{
“id”: 0,
“name”: “”,
“language_id”: 0,
“parent_id”: 0,
“image_id”: 0,
“display_index”: 0,
“release”: false,
“created_at”: “2022-03-25T02:57:43.965Z”,
“updated_at”: “2022-03-25T02:57:43.965Z”
}
Second error if I change the attrbute from “language_id” to “language”
{
“detail”: [
{
“loc”: [
“body”,
“data”,
“language_id”
],
“msg”: “field required”,
“type”: “value_error.missing”
}
]
}
#data.dict().keys # major_cd = Major.objects.get(major_cd=int(major_cd)),