使用python django中的put方法更新所需的领域

时间:2019-03-14 10:13:36

标签: python-2.7 django-rest-framework

当我想要更新一个字段时,我必须将所有要输入的字段以及要更新的字段提供给我。如何更新一个字段? 领域,而没有提供其他领域的投入?

views.py

''''def put_artist(request, id):

    a_artist = Artist.objects.filter(id=id).values()
    if a_artist is None:
    return failure("artist with id does not exist")
else:

    data = json.loads(request.body.decode('utf-8'))
    hero_name = data["hero_name"]
    heroine_name = data["heroine_name"]
    numberofcrew = data["numberofcrew"]

Artist.objects.filter(id = id).update(hero_name = hero_name,heroine_name = heroine_name,numberofcrew = numberofcrew)

    return success( "song with id : {} is updated".format(id),data)''''

models.py

''''class Artist(models.Model):
    hero_name=models.CharField(max_length=20)
    heroine_name=models.CharField(max_length=20)
    numberofcrew=models.IntegerField()

    objects=models.Manager()

    def __unicode__(self):
        return '%s' % self.hero_name

    class Meta:
        ordering = ["hero_name"]
        verbose_name = "Artist"
        verbose_name_plural = "Artists" ''''

0 个答案:

没有答案