这是一个正常的表单填写代码,我在其中创建了表单并将 request.post 传递给它。发布请求数据已正确捕获,但在传递到表单后未保存。django 3.1 版是基础语言。
===view===
def adst(request):
lists = Site.objects.filter(deleted=False)
country = Country.objects.filter(deleted=False)
company = Company.objects.filter(deleted=False)
region = Region.objects.filter(deleted=False)
location= Location.objects.filter(deleted=False)
sbu = SBU.objects.filter(deleted=False)
delta = {'status': True}
if request.method == 'POST':
print(request.POST) # Print_Output
request.POST = request.POST.copy()
request.POST['inserted_by'] = request.user.id
form = SiteForm(request.POST)
**if form.is_valid():**
form.save()
print(request.GET) # Print_Output
context = {'form' : SiteForm(), 'list' : lists,
'data' : delta, 'sbu' : sbu,'country' : country,
'company' : company, 'region' : region,
'location' : location}
return render(request, 'masters/site.html', context)
===Post Data===
<QueryDict: {'csrfmiddlewaretoken': ['sdtbHHn5OVAf19kkaJUlijHzURZEHNNLKm7PLnR6bxrodIKH4rbgB'], 'country': ['India'], 'company': ['GILI'], 'region': ['north'], 'location': ['Vikhroli'], 'sbu': ['goejo nw'], 'site': ['asdasd'], 'status': ['on']}>
===Form===
class SiteForm(ModelForm):
class Meta:
model = Site
fields = ['id','code','site', 'country', 'company', 'region', 'location', 'sbu', 'status','deleted', 'inserted_by', 'updated_by']
===Model===
class Site(models.Model):
code = models.CharField(max_length=250, null=True, blank=True, db_column='code')
site = models.CharField(max_length=250, null=True, db_column='site')
country = models.ForeignKey(Country, null=True, blank=True, on_delete=models.CASCADE)
company = models.ForeignKey(Company, null=True, blank=True, on_delete=models.CASCADE)
region = models.ForeignKey(Region, null=True, blank=True, on_delete=models.CASCADE)
location = models.ForeignKey(Location, null=True, blank=True, on_delete=models.CASCADE)
sbu = models.ForeignKey(SBU, null=True, blank=True, on_delete=models.CASCADE)
status = models.BooleanField(default=True, db_column='status')
deleted = models.BooleanField(default=False, null=True, blank=True, db_column='deleted')
inserted_by = models.BigIntegerField(null=True, blank=True, db_column='inserted_by')
inserted_on = models.DateTimeField(auto_now_add=True, db_column='inserted_on')
updated_by = models.BigIntegerField(null=True, blank=True, db_column='updated_by')
updated_on = models.DateTimeField(auto_now=True, db_column='updated_on')
def __str__(self):
return self.site
def datamodify(self):
return self.inserted_on >= timezone.now() - datetime.timedelta(days=1)
return self.updated_on >= timezone.now() - datetime.timedelta(days=1)