Django if statements в формах

Я пытаюсь позволить пользователю загрузить файл неизвестного типа через форму, а затем сохранить его в папке в зависимости от типа файла. Я надеялся, что смогу использовать операторы 'if', но я не могу заставить их работать внутри формы. В настоящее время у меня есть только прямые пути загрузки:

class Post(models.Model):
    Priority_Upload = models.FileField(default='priority', upload_to='priority/', blank=True, null=True)
    title = models.CharField(max_length=100)
    content = models.FileField(default='text', upload_to='text/', blank=True, null=True)
    image = models.ImageField(default='default', upload_to='images/', blank=True, null=True)
    video = models.FileField(default='video', upload_to='videos/',blank=True, null=True)
    large_video = models.FileField(default='large_video', upload_to='large_video/', blank=True, null=True)
    date_posted = models.DateTimeField(default=timezone.now)
    # user owns the post, but post doesn't own the user.

    author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='+')
    def __str__(self):
        return self.title
    def get_absolute_url(self):
        return reverse('post-detail', kwargs={'pk': self.pk})

Я хотел бы сделать что-то вроде этого, но не получается:

class Post(models.Model):
    Priority_Upload = models.FileField(default='priority', upload_to='priority/', blank=True, null=True)
    title = models.CharField(max_length=100)
    if(*image ends in .txt*)
        content = models.FileField(default='text', upload_to='text/', blank=True, null=True)
    if(*image ends in .png*)
        image = models.ImageField(default='default', upload_to='images/', blank=True, null=True)
    ... 

есть ли способ сделать это?

Вы можете попробовать как

if str(image).endswith('txt'):
   *Your condition*
Вернуться на верх