0
models.py
......
class CatalogGroup(models.Model):
name = models.CharField(max_length=100, blank=True, null=True)
def __unicode__(self):
return self.name
class Place(models.Model):
name = models.CharField(max_length=100, blank=True, null=True)
def __unicode__(self):
return self.name
class Scale(models.Model):
name = models.CharField(max_length=100, blank=True, null=True)
def __unicode__(self):
return self.name
class ProductsTbl(models.Model):
model_number = models.CharField(max_length=255, blank=True, unique=True,error_messages={'unique':"This model number has already been registered."})
name = models.CharField(max_length=255, blank=True, null=True)
material = models.CharField(max_length=255,blank=True, null=True)
color = models.CharField(max_length=255, blank=True, null=True)
feature = models.TextField(blank=True, null=True)
created = models.DateTimeField(editable=False)
modified = models.DateTimeField(auto_now=True)
release = models.DateTimeField(blank=True, null=True)
suggest = models.TextField(blank=True, null=True)
description = models.TextField(blank=True, null=True)
cataloggroup = models.ManyToManyField(CatalogGroup)
place = models.ManyToManyField(Place)
scale = models.ManyToManyField(Scale)
slug = models.SlugField(unique=True)
user = models.ForeignKey(User, blank=True, null=True)
useredit = models.CharField(max_length=32, blank=True, null=True)
image = models.ImageField(upload_to=get_imagep_Product, blank=True)
def __unicode__(self):
return self.name
def save(self, *args, **kwargs):
''' On save, update timestamps '''
if not self.id:
self.created = timezone.now()
return super(ProductsTbl, self).save(*args, **kwargs)
.....
,我需要的,如果任何用戶修改表單中的數據更新數據庫,所以我有:
views.py
.......
@login_required
def edit_thing(request, slug):
# grab the object...
thing = ProductsTbl.objects.get(slug=slug)
if thing.user != request.user:
raise Http404
# set the form we're using...
form_class = ProductsTblForm
if request.method == 'POST':
# grab the data from the submitted form
form = form_class(data=request.POST,files=request.FILES,instance=thing)
if form.is_valid():
form = form.save(commit=False)
form.useredit = request.user.username
form.save()
return redirect('thing_detail', slug=thing.slug)
# otherwise just create the form
else:
form = form_class(instance=thing)
# and render the template
return render(request, 'things/edit_thing.html', {
'thing': thing,
'form': form,
})
.........
不過,我submited數據後,它只有部分數據可在數據庫保存,下面這三個數據不能保存
cataloggroup = models.ManyToManyField(CatalogGroup)
place = models.ManyToManyField(Place)
scale = models.ManyToManyField(Scale)
我不知道爲什麼這三個數據無法保存在??
,如果我除去
形式= form.save(提交=假)
從
views.py
,所述
form.useredit = request.user.username
將無法正常工作,但能這三個數據保存到數據庫,但我必須讓USEREDIT更新到用戶誰修改的數據。