Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.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 save()方法在使用相同id保存时,将某个字段填充为None_Python_Django_Sqlite_Django Models_Timezone - Fatal编程技术网

Python Django save()方法在使用相同id保存时,将某个字段填充为None

Python Django save()方法在使用相同id保存时,将某个字段填充为None,python,django,sqlite,django-models,timezone,Python,Django,Sqlite,Django Models,Timezone,我有这个型号 class Country(models.Model): countryname = models.TextField(null=True) countryincharge = models.ForeignKey(Employees, on_delete=models.CASCADE,null = True) createdby = models.TextField(null=True) createdtime = models.TextField(null=True) modi

我有这个型号

class Country(models.Model):

countryname = models.TextField(null=True)
countryincharge = models.ForeignKey(Employees, on_delete=models.CASCADE,null = True)
createdby = models.TextField(null=True)
createdtime = models.TextField(null=True)
modifiedby = models.TextField(null=True)
modifiedtime = models.TextField(null=True)
deletedby = models.TextField(null=True)
deletedtime = models.TextField(null=True)
isdeleted = models.IntegerField(null=True)
tenantid = models.TextField(null=True)
并创建和更新函数,如下所示

#Create
def countrycreate(request):

if request.user.is_authenticated:

    if request.method == 'POST':
        id = request.POST.get('id')

        modelwithdata = Country(
            countryname=request.POST.get('countryname'),
            countryincharge_id=request.POST.get('countryincharge'),
            createdtime = timezone.now(),
            createdby = request.user.id


        )
        if Country.objects.filter(countryname=request.POST.get('countryname')).exists():
            messages.info(request, 'Country already exists!')
            return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

        else:
            modelwithdata.save()

        return redirect('/admin/countrylistview')

 #Update
 def countryupdate(request):

if request.user.is_authenticated:

    if request.method == 'POST':
        id = request.POST.get('id')
        modelwithdata = Country(
            countryname=request.POST.get('countryname'),
            countryincharge_id=request.POST.get('countryincharge'),
            modifiedtime = timezone.now(),
            modifiedby = request.user.id
        )

        modelwithdata.id = id
        modelwithdata.save()
        return redirect('/admin/countrylistview')

问题是当创建记录时,createdtime值会按预期保存。但在运行update函数时,createdtime值设置为None。不知什么原因。其他领域运作良好。数据库是SQLite。当使用
DateTimeField(auto\u now\u add=True,null=True)
DateTimeField(auto\u now=True,null=True)
时也会发生同样的行为。问题是您正在创建一个新对象,首先抓取原始对象并仅更改所需的内容

#Update
// Change this to get the object
modelwithdata = get_object_or_404(Country, pk=id)

// Change only the fields you need to
modelwithdata.countryname=request.POST.get('countryname')

modelwithdata.countryincharge_id=request.POST.get('countryincharge')
modelwithdata.modifiedtime = timezone.now()
modelwithdata.modifiedby = request.user.id

//Dont need this
//   modelwithdata.id = id
//Save the new model
modelwithdata.save()

问题是,在创建新对象时,首先获取原始对象并仅更改所需的内容

#Update
// Change this to get the object
modelwithdata = get_object_or_404(Country, pk=id)

// Change only the fields you need to
modelwithdata.countryname=request.POST.get('countryname')

modelwithdata.countryincharge_id=request.POST.get('countryincharge')
modelwithdata.modifiedtime = timezone.now()
modelwithdata.modifiedby = request.user.id

//Dont need this
//   modelwithdata.id = id
//Save the new model
modelwithdata.save()

您应该使用
PUT
PATCH
方法更新数据,而不是POST

对于您的代码,我们也可以使用
.update()


您应该使用
PUT
PATCH
方法更新数据,而不是POST

对于您的代码,我们也可以使用
.update()