2

我正在尝试通过 POST 调用迭代地设置模型中的各个字段,而无需编写一堆单独的 set 和 get 方法。不幸的是,我概念化的唯一方法看起来非常老套,我什至无法让它发挥作用。我觉得我正在做一些艰难的事情或与语言的设计作斗争,并希望有人能指出我正确的方向:

我的修改功能

def modify(request, specification_id):
    spec = get_object_or_404(Specification, pk=specification_id)
    user = request.user
    try:
        for key, value in request.POST.iteritems():
            if spec._get_FIELD_display(spec._meta.get_field_by_name(key)[0]) != value:
                #This would be where the setter would go if I could figure out how to make it work
class Specification(models.Model):
    text = models.TextField(max_length=40000)
    last_modified = models.DateTimeField('Date Last Modified')
4

0 回答 0