提交表单时,我想将已分类的实例保存到classifiedMessage
模型,该模型是从提交表单的页面slug中检索的。
forms.py
class ClassifiedContactForm(ModelForm):
captcha = ReCaptchaField()
class Meta:
model = ClassifiedMessage
exclude = ["classified"]
models.py
class ClassifiedMessage(models.Model):
classified = models.ForeignKey('classified')
name = models.CharField(max_length=50)
email = models.EmailField()
message = models.TextField(max_length=300)
def __unicode__(self):
return "%s"%(self.name)
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 ('classified-detail', (),{'slug' :self.slug})
def save(self, *args, **kwargs):
if not self.slug:
slug_str = "%s %s %s %s" % (self.title, self.city, self.state, self.zipcode)
self.slug = slugify(slug_str)
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)
views.py
def classified_detail(request,slug):
classifiedinst = classified.objects.get(slug=slug)
classifiedimg = ClassifiedImage.objects.filter(classified=classifiedinst)
'''Create a string representation of classified's address so it can be geocoded'''
classified_address = "%s %s %s %s"%(classifiedinst.address, classifiedinst.city, classifiedinst.state, classifiedinst.zipcode)
lat, lng = getLatLng(classified_address)
constructed_map = getMap(lat, lng, largeMap=False)
if request.POST:
form = ClassifiedContactForm(request.POST)
if form.is_valid():
x = form.save(commit=False)
x.classified=classifiedinst
x.save()
return HttpResponseRedirect('')
else:
form = ClassifiedContactForm(initial={'classified':classifiedinst.pk})
return render_to_response('shclassified/classified_detail.html',
{
'ClassifiedContactForm':form,
'imgObjects':classifiedimg,
'user':request.user,
'classified':get_object_or_404(classified,slug=slug),
'constructed_map':constructed_map,
},
context_instance = RequestContext(request)
)
错误:
psycopg2.InternalError
InternalError: current transaction is aborted, commands ignored until end of transaction block
Postgresql日志:
2013-12-24 21:50:57 EST ERROR: column "classified_id" of relation "shclassified_classifiedmessage" does not exist at character 47
2013-12-24 21:50:57 EST STATEMENT: INSERT INTO "shclassified_classifiedmessage" ("classified_id", "name", "email", "message") VALUES (5, 's', 'test@gmail.com', 'test') RETURNING "shclassified_classifiedmessage"."id"
2013-12-24 21:50:57 EST ERROR: current transaction is aborted, commands ignored until end of transaction block
2013-12-24 21:50:57 EST STATEMENT: SHOW default_transaction_isolation
shclassified_classifiedmessage表
Column | Type | Modifiers | Storage | Description
---------+-----------------------+-----------------------------------------------------------------------------+----------+-------------
id | integer | not null default nextval('shclassified_classifiedmessage_id_seq'::regclass) | plain |
name | character varying(50) | not null | extended |
email | character varying(75) | not null | extended |
message | text | not null | extended |
Indexes:
"shclassified_classifiedmessage_pkey" PRIMARY KEY, btree (id)
Has OIDs: no
Sqlreset输出:
BEGIN;
DROP TABLE "shclassified_classifiedimage";
ALTER TABLE "shclassified_classifiedmessage" DROP CONSTRAINT "classified_id_refs_id_4e71e9af";
DROP TABLE "shclassified_classified";
DROP TABLE "shclassified_classifiedmessage";
CREATE TABLE "shclassified_classifiedmessage" (
"id" serial NOT NULL PRIMARY KEY,
"classified_id" integer NOT NULL,
"name" varchar(50) NOT NULL,
"email" varchar(75) NOT NULL,
"message" text NOT NULL
)
;
CREATE TABLE "shclassified_classified" (
"id" serial NOT NULL PRIMARY KEY,
"slug" varchar(50) UNIQUE,
"submitted_by_id" integer REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED,
"title" varchar(120),
"address" varchar(120),
"city" varchar(60),
"state" varchar(60),
"zipcode" varchar(5),
"description" text,
"submission_date" timestamp with time zone,
"price" varchar(20)
)
;
ALTER TABLE "shclassified_classifiedmessage" ADD CONSTRAINT "classified_id_refs_id_4e71e9af" FOREIGN KEY ("classified_id") REFERENCES "shclassified_classified" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE TABLE "shclassified_classifiedimage" (
"id" serial NOT NULL PRIMARY KEY,
"classified_id" integer REFERENCES "shclassified_classified" ("id") DEFERRABLE INITIALLY DEFERRED,
"image" varchar(100)
)
;
CREATE INDEX "shclassified_classifiedmessage_classified_id" ON "shclassified_classifiedmessage" ("classified_id");
CREATE INDEX "shclassified_classified_submitted_by_id" ON "shclassified_classified" ("submitted_by_id");
SELECT AddGeometryColumn('shclassified_classified', 'point', 4326, 'POINT', 2);
CREATE INDEX "shclassified_classified_point_id" ON "shclassified_classified" USING GIST ( "point" GIST_GEOMETRY_OPS );
CREATE INDEX "shclassified_classifiedimage_classified_id" ON "shclassified_classifiedimage" ("classified_id");
COMMIT;