出于某种原因,我可以创建一个新的分类模型实例,但无法通过我的视图或Django中的管理面板更新现有的实例。我使用postgresql并从日志中,似乎查询根本没有访问数据库。我删除并重置了应用程序,甚至删除了表但没有帮助。
class classified(models.Model):
slug = models.SlugField(unique=True,blank=True, null=True)
submitted_by = models.ForeignKey(User, blank=True, null=True)
title = models.CharField(max_length=120, blank=True, null=True)
point = models.PointField(srid=settings.SRID, blank=True, null=True)
address = models.CharField(max_length=120, blank=True, null=True)
city = models.CharField(max_length=60, blank=True, null=True)
state = models.CharField(max_length=60, blank=True, null=True)
zipcode = models.CharField(max_length=5, blank=True, null=True)
description = models.TextField(max_length=500,blank=True, null=True)
objects = models.GeoManager()
tags = TaggableManager(blank=True)
submission_date = models.DateTimeField(auto_now_add=True, null=True, blank=True)
price = models.CharField(max_length=20, blank=True, null=True)
def __unicode__(self):
#return "%s %s %s"%(self.title, self.point.x, self.point.y)
return "%s"%(self.title)
@models.permalink
def get_absolute_url(self):
return ('listing_detail', (),
{
'slug' :self.slug,
})
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
if not self.point:
location = "%s+%s+%s+%s"%(self.address, self.city, self.state, self.zipcode)
cord = get_lat_long(location)
x,y = cord.split(",")
x = float(x)
y = float(y)
self.point = Point(x,y)
self.point2 = Point(x,y)
super(classified, self).save(*args, **kwargs)
ClassifiedImage模型:
class ClassifiedImage(models.Model):
classified = models.ForeignKey(classified, related_name="images", null=True, blank=True)
image = models.ImageField(upload_to='classifieds/%Y/%m/%d', default='static/img/no-thumb.jpg', null=True, blank=True)
分类视图:
def add_classified(request):
userprofile = User.objects.get(pk=request.user.id)
if request.method == 'POST':
form = classifiedForm(request.POST, request.FILES)
if form.is_valid():
classifiedad = form.save(commit=False)
image_formset = ImageFormSet(request.POST, request.FILES, instance=classifiedad)
classifiedad.submitted_by = request.user
classifiedad.save()
image_formset.save()
slug = classifiedad.slug
redirect_to =reverse('classified-detail', kwargs={'slug':slug})
return HttpResponseRedirect(redirect_to)
else:
form = classifiedForm()
image_formset = ImageFormSet()
return render_to_response('shclassified/add_classified.html',{'form':form, 'image_formset':image_formset},context_instance = RequestContext(request))
分类更新视图:
def ClassifiedUpdate(request, slug):
classifiedins = classified.objects.get(slug=slug)
if request.method == 'POST':
form = classifiedForm(request.POST, request.FILES)
if form.is_valid():
#classifiedad = form.save(commit=False)
image_formset = ImageFormSet(request.POST, request.FILES, prefix="images", instance=classifiedins)
#classifiedad.submitted_by = request.user
#classifiedad.save()
#form().save()
image_formset.save()
#slug = classifiedad.slug
return HttpResponseRedirect(".")
else:
form = classifiedForm(instance=classifiedins)
image_formset = ImageFormSet(instance=classifiedins, prefix="images")
return render_to_response('shclassified/add_classified.html',{'form':form, 'image_formset':image_formset},context_instance = RequestContext(request))
答案 0 :(得分:1)
你对事物的外观错误地覆盖了save
方法:
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
if not self.point:
location = "%s+%s+%s+%s"%(self.address, self.city, self.state, self.zipcode)
cord = get_lat_long(location)
x,y = cord.split(",")
x = float(x)
y = float(y)
self.point = Point(x,y)
self.point2 = Point(x,y)
super(classified, self).save(*args, **kwargs) ## wrong indentation!
您的super()
来电应与第一级缩进对齐。如果没有super().save
,只有在没有self.point
时才会调用{。}}。