我有一个名为“classifieds”的django模型,顾名思义就是一个分类数据库。每个分类实例都会附加多个图像。因此创建了另一个名为“ClassifiedImage”的模型,并通过外键与“分类”模型相关联。我想利用Django通用更新视图来更新“分类”模型,但是通用视图只接受一个模型和表单实例(请参见下面的*)。但目前通用视图允许我只显示1个表单(在本例中为“classifiedForm”),我可以更新分类模型但不能编辑该特定实例的图像。请记住,依赖于图像存储和链接的classifiedImage模型外键。
views.py
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 classified_list(request):
classified_list = classified.objects.all()
return render_to_response('classified_list.html',
{'classified_list':classified_list},
context_instance = RequestContext(request)
)
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)
#Get user's full name
return render_to_response('shclassified/classified_detail.html',
{
'classifiedimg':classifiedimg,
'user':request.user,
'classified':get_object_or_404(classified,slug=slug),
'constructed_map':constructed_map,
},
context_instance = RequestContext(request)
)
#I NEED TO BE ABLE TO UPDATE THE IMAGES HERE IN THIS VIEW AS WELL. DELETE, REPLACE #IMAGES ETC...Perhaps generic views are not designed for this purpose?
class ClassifiedUpdate(UpdateView):
model = classified# ****** <=== ***
form_class = classifiedForm **** <=== ***
template_name = 'shclassified/add_classified.html'
Models.py
**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(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")
#image = ImageField()
image = models.ImageField(upload_to='classifieds/%Y/%m/%d', default='static/img/no-thumb.jpg', null=True, blank=True)**
Forms.py
class classifiedForm(ModelForm):
class Meta:
model = classified
exclude = ["user","point","submitted_by","slug"]
ImageFormSet = inlineformset_factory(classified, ClassifiedImage, can_delete = False)