Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 迭代模型字段并在Django中更新它们_Python_Django_Class_Loops_Model - Fatal编程技术网

Python 迭代模型字段并在Django中更新它们

Python 迭代模型字段并在Django中更新它们,python,django,class,loops,model,Python,Django,Class,Loops,Model,我有一个名为UserInfo的模型 class UserInfo(models.Model): username = models.CharField(max_length=240 , default = "" , blank = False , null = False) first_name = models.CharField(max_length=240 , default = "" , blank = False , null = False) last_nam

我有一个名为
UserInfo
的模型

class UserInfo(models.Model):
    username = models.CharField(max_length=240 , default = "" , blank = False , null = False)
    first_name = models.CharField(max_length=240 , default = "" , blank = False , null = False)
    last_name = models.CharField(max_length=240 , default="" , blank = False , null = False)
    address = models.CharField(max_length=500 , default="" , blank = False , null = False)
    email = models.CharField(max_length=240 , default="" , blank = False , null = False)
    phoneNumber = models.CharField(max_length=240 , default="" , blank = False , null = False)
    pincode = models.CharField(max_length=240 , default="" , blank = False , null = False)
我还有一个包含这些字段的
UserInfoForm

class UserInfoForm(forms.ModelForm):
    class Meta:
        model = UserInfo
        fields = []
        for field in UserInfo._meta.get_fields(): #automatically update fields from userinfo model
            fields.append(field.name)
        exclude = ['username']
我们想要的是
UserInfo
模型的字段上迭代,并使用
UserInfoForm
中的数据进行更新,而不是全部硬编码

我试过这个:

obj = UserInfo.objects.get(email = request.user.email)
if obj.username == request.user.username: #basically a test to see if the same person is updating his profile, or is someone else using this email id
    for field in UserInfo._meta.get_fields():

        fieldname = field.name
        fieldvalue = form.cleaned_data.get(fieldname) 

        obj.field = fieldvalue #doesn't work
    obj.save()

但这不起作用。这段代码没有显示任何错误,但数据库仍然没有更新。请建议一种方法,以便我可以通过迭代字段来更新用户信息。

在您的方法中,您可能只需要一个
obj.save()
。但是在更广泛的范围内,是否有理由不只是使用
form.save()
?@FamousJameous form.save正在创建重复项。但是我不希望数据库中有多个人具有相同的电子邮件id。我认为惯例是创建和更新的表单不同。对于更新案例,在发送到客户机之前,将现有实例传递给表单构造函数(以便表单字段填充现有数据)。然后,在发布表单时,再次传入实例(通常通过URL中的ID获得)。然后,当您调用
form.save()
时,它只更新现有实例。有关详细信息,请参阅。@FamousJameous好的,我试试。谢谢你的帮助