1

出于某种原因,我可以创建分类模型的新实例,但无法通过我的视图或 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)

分类图像模型:

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))
4

1 回答 1

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.

于 2013-07-03T00:32:56.270 回答