更新模型和序列化器后,我无法将新文章保存到数据库中。我不断收到“'title'是此函数的无效关键字参数。”
我正在研究Postgres10,Python3.6,Django2.0和DRF 3.8.2。
我删除了我的数据库几次,但错误仍然存在。拜托我需要你的帮忙。提前致谢。
这是我的旧型号的副本
'remove'
我的新(更新)型号:
class Article(models.Model):
title = models.CharField(max_length=200, blank=False)
slug = models.SlugField(max_length=250, unique_for_date='created')
body = models.TextField()
image_url = models.URLField(max_length=255)
author = models.ForeignKey(User, on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add = True)
published = models.DateTimeField(auto_now=True)
updated = models.DateTimeField(auto_now=True)
country = models.ForeignKey(Country, on_delete=models.CASCADE)
categories = models.ManyToManyField(Category, default=1)
# categories = ArrayField(models.IntegerField(), blank=False)
entities = models.ManyToManyField(Entity, default=1)
# entities = ArrayField(models.CharField(max_length=200),
blank=True)
summary = models.TextField(blank=True)
is_published = models.BooleanField(default=False)
is_curated = models.BooleanField(default=False)
class Meta:
ordering = ('-created',)
def __str__(self):
return self.title
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super(Article, self).save(*args, **kwargs)
我的序列化器:
class Article(models.Model):
title = models.CharField(max_length=200, blank=False)
slug = models.SlugField(max_length=250, unique_for_date='created')
body = models.TextField()
image_url = models.URLField(max_length=255)
author = models.ForeignKey(User, on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add = True)
published = models.DateTimeField(auto_now=True)
updated = models.DateTimeField(auto_now=True)
country = models.ForeignKey(Country, on_delete=models.CASCADE,
related_name="posts")
category = models.OneToOneField(Category,
on_delete=models.CASCADE,
default=1, related_name="posts")
entities = models.ManyToManyField(Entity, default=1,
related_name="posts")
summary = models.TextField()
keypoint1 = models.CharField(max_length=500, blank=False,
null=False)
keypoint2 = models.CharField(max_length=500, blank=False,
null=False)
keypoint3 = models.CharField(max_length=500, blank=True,
null=True)
keypoint4 = models.CharField(max_length=500, blank=True,
null=True)
is_published = models.BooleanField(default=False)
is_curated = models.BooleanField(default=False)
class Meta:
ordering = ('-created',)
def __str__(self):
return self.title
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super(Article, self).save(*args, **kwargs)
我的创建帖子视图:
class CreateArticleSerializer(serializers.ModelSerializer):
author = UserSerializer(read_only=True, required=False)
country = CountrySerializer(read_only=True)
class Meta:
model = Article
fields = ("id", "title", "body", "image_url", "country",
"keypoint1", "keypoint2", "keypoint3", "keypoint4",
"category", "entities", "author", "created", "updated",
"published", "is_published")
read_only_fields = ("id", "created", "slug")
def create(self, validated_data):
entities_relations = validated_data.pop('entities', None)
entity_instance = Entity.objects.create(**validated_data)
entity_instance.posts = entities_relations
article = Article.objects.create(entities=entity_instance,
**validated_data)
return article
答案 0 :(得分:0)
在序列化程序的create方法上,您尝试使用Entity
的数据字段创建一个Article
:
entities_relations = validated_data.pop('entities', None)
entity_instance = Entity.objects.create(**validated_data)
您没有指定发生错误的行,但似乎在这里。我不知道Entity
模型的字段,但标题似乎不是其中之一。
答案 1 :(得分:0)
您确定
entity_instance = Entity.objects.create(**validated_data)
?? 实体模型是否接受与第一条相同的字段?