4

名前が示すように、分類されたデータベースである「分類済み」と呼ばれるdjangoモデルがあります。分類済みの各インスタンスには、複数の画像が添付されます。そのため、「ClassifiedImage」という名前の別のモデルを作成し、外部キーを介して「分類済み」モデルに関連付けました。Django ジェネリック更新ビューを利用して「分類済み」モデルを更新したいのですが、ジェネリック ビューは 1 つのモデルとフォーム インスタンスしか受け入れません (以下を参照してください *)。しかし、現在、一般的なビューでは、分類されたモデルを更新できるが、その特定のインスタンスの画像を編集できない 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)**

Forms.py

class classifiedForm(ModelForm):
      class Meta:
          model = classified
          exclude = ["user","point","submitted_by","slug"]

ImageFormSet = inlineformset_factory(classified, ClassifiedImage, can_delete = False)
4

0 に答える 0