from django.db.models.signals import pre_save
from django.utils.text import slugify
from django.db import models
from django.conf import settings
from django import forms
# Create your models here.
class Video(models.Model):
Video_Description= models.CharField(max_length=500)
slug = models.SlugField(unique=True)
videofile= models.FileField(upload_to='videos/', null=True, verbose_name="")
timestamp = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['-timestamp']
def get_absolute_url(self):
return reverse ("video:detail", kwargs={"slug":self.slug})
def __str__(self):
return self.Video_Description + ": " + str(
self.id)
def create_slug(instance, new_slug=None):
slug = slugify(instance.Video_Description)
if new_slug is not None:
slug = new_slug
qs = Video.objects.filter(slug=slug).order_by("-id")
exists = qs.exists()
if exists:
new_slug = "%s-%s" %(slug, qs.first().id)
return create_slug(instance, slug=new_slug)
return slug
def pre_save_video_receiver(sender, instance, *args, **kwargs):
if not instance.slug:
instance.slug = create_slug(instance)
pre_save.connect(pre_save_video_receiver, sender=Video)
The problem I am facing is, slug field appears in the admin and unfortunately in the create page where I am uploading the videos.
However, in the url it is not coming, also in the create page, I have to enter the slug manually. I want it to take whatever input was given to t video_description in the models.py file and it should disappear from the create post page.
Kindly help me.