2016-09-14 112 views
0

我想限制上傳文件上的用戶數量。例如: 每天,用戶被限制爲1個圖像文件。Django上傳限制

查看

class Upload(views.LoginRequiredMixin, generic.CreateView): 
model = Posts 
form_class = UploadForm 
template_name = 'icerik_yukle.html' 

def form_valid(self, form): 
    self.object = form.save(commit=False) 
    self.object.author= self.request.user 
    self.object.save() 
    return super(Upload, self).form_valid(form) 

模式

class Gonderi(models.Model): 
    author= models.ForeignKey(User, related_name="gonderi") 
    slug = models.SlugField(unique=True, max_length=10, default=id_olustur) 
    image = models.FileField(upload_to=yukleme_adresi, blank=True) 
    subject= models.CharField(max_length=50, blank=True) 
    descrip = models.TextField(max_length=250, blank=True) 
    category= models.ForeignKey(Kategori, verbose_name="Kategori") 
    tags = TaggableManager() 
    created= models.DateTimeField(auto_now_add=True) 
    updated= models.DateTimeField(auto_now=True) 
    is_public = models.BooleanField(verbose_name='Göster', default=True) 

回答

2

檢查是否存在當天的記錄,如果有用戶的有效畫面。如果有,提出例外

def form_valid(self, form): 
    from datetime import datetime 
    query = Gonderi.objects.filter(created__date=datetime.now().date(), 
            author=request.user) 
    instance = query[0] if query.count() > 0 else None 
    if instance and instance.image: 
     #raise your error over here 


    self.object = form.save(commit=False) 
    self.object.author= self.request.user 
    self.object.save() 
    return super(Upload, self).form_valid(form) 
+0

嗨,謝謝你的回答。我嘗試了代碼,但沒有工作。上傳可以沒有限制地完成。 –

+0

@SaadettinYasirAKEL你是否例外? – levi

+0

我嘗試過這種方式。我爲新手感到抱歉。 'def form_valid(self,form): sorgu = Gonderi.objects.filter(olusturma_zamani = datetime.now()。date(), sanatci = self.request.user) instance = sorgu [0] if sorgu.count ()> 0 else None if instance and instance.resim: raise Exception(「Günlüklimitiniz doldu。」) self.object = form.save(commit = False) self.object.sanatci = self.request.user self.object.save()' –