0

I want to make've limited the number of users on the upload file. For example: Every day, users are limited to 1 image file.

Views

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)

Models

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)

1 Answer 1

2

Check if exists a record for the current day and if it has a valid picture for the user. If there is, raise an exception

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, Thank you for your answer. I tried the code but did not work. Upload can be done without limit.
@SaadettinYasirAKEL are u rising the exception ?
I have tried in this way. I'm sorry for the novice. ` 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ük limitiniz doldu.") self.object = form.save(commit=False) self.object.sanatci = self.request.user self.object.save()`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.