I have a model defined and over 100+ entries of data in my DB. I would like to auto populate a slug field and see it show up in the admin, since adding new entries for 100+ fields is not something I would like to do.
AutoSlug() field doesnt seem to be working when I add it to my model and make the migrations, prepopulated_fields = {'slug': ('brand_name',)}
does not work using it within my admin.py
and as well I have tried to add the default field on the slug as my desired field name within the Model but to no avail the solution didnt work.
Is their any other suggestions on how to get the slug filed pre-populated?
class Brand(models.Model):
brand_name = models.CharField(unique=True, max_length=100, blank=True, default="", verbose_name=_('Brand Name'))
slug = models.SlugField(max_length=255, verbose_name=_('Brand Slug'), default=brand_name)
If you have built your own server-rendered interface with forms, you could auto-populate the fields by using either the |slugify tamplate filter or the slugify utility when saving the form (is_valid).
The last step is to add the link in HTML file <a href=”/posts/{{ a. slug }}” class=”btn btn-primary”>View</a>. Now we are ready to go to 127.0. 0.1:8000/posts/title-you-have-added and it will show you the page details.
For creating unique slug in Django, we will be using Slugify and Django Signals. But first we will create a function for Creating a Slug using first_name and if that first_name is present in slug field, we will attach unique string to slug.
A slug field in Django is used to store and generate valid URLs for your dynamically created web pages. Just like the way you added this question on Stack Overflow and a dynamic page was generated and when you see in the address bar you will see your question title with "-" in place of the spaces.
You can try adding a save method to the Brand class.
from django.utils.text import slugify
class Brand(models.Model):
...
def save(self, *args, **kwargs):
self.slug = slugify(self.brand_name)
super(Brand, self).save(*args, **kwargs)
then run:
python manage.py shell
>>>from app.models import Brand
>>>brands = Brands.objects.all()
>>>for brand in brands:
>>> brand.save()
Also, I would change brand_name var to just name.
I think it's better to add a pre_save
signal on the Brand
model.
from django.dispatch import receiver
from django.db.models import signals
from django.contrib.auth import get_user_model
from django.utils.text import slugify
@receiver(signals.pre_save, sender=<model name>)
def populate_slug(sender, instance, **kwargs):
instance.slug = slugify(instance.brand_name)```
I think I have an idea, which would do the job, however I am not sure if that would be the best way to do it.
I would use slugify
function. I would create the view which - after it was called - would get all model's objects, iterate over them and populate each model's slug field using django.utils.text.slugify
function with model's brand_name
as a value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With