迭代访问和设置模型字段

时间:2012-12-14 04:21:51

标签: django django-models

我正在尝试从POST调用迭代地设置模型中的各个字段,而不需要编写一堆单独的set和get方法。不幸的是,我概念化的唯一方法看起来非常h​​acky,我甚至无法使其工作。我觉得我正在以艰难的方式做事或与语言设计作斗争,希望有人能指出我正确的方向:

我的修改功能

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')

0 个答案:

没有答案