我有一个名为“分类”的 django 模型,顾名思义,它是一个分类数据库。分类的每个实例都将附加多个图像。因此创建了另一个名为“ClassifiedImage”的模型,并通过外键关联到“分类”模型。我想利用 Django 通用更新视图来更新“分类”模型,但通用视图只接受一个模型和表单实例(请参见下文 *)。但目前通用视图只允许我显示 1 个表单(在本例中为“classifiedForm”),我可以更新分类模型但无法编辑该特定实例的图像。请记住,存储图像并通过以下方式链接的分类图像模型外键。
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'
模型.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)**
表格.py
class classifiedForm(ModelForm):
class Meta:
model = classified
exclude = ["user","point","submitted_by","slug"]
ImageFormSet = inlineformset_factory(classified, ClassifiedImage, can_delete = False)