Monday, July 1, 2019

How to use model property(FK) instead of pk in prepopulated_fields

Hello all,

I want to create a unique property slug for all my category. Categories are: HTML & Wordpress. I want to create property slug as {property_name}-{category} (e.g. files-html, files-worpdress, is-documented-html, is-documented-wordpres..)


Now problem is when I use below code snippet in my ModelAdmin, it gives me like this: (files-5, files-7...)
Above digit after property_name is id of that category which is a foreign key in my property model.

prepopulated_fields = {'property_slug': ('property_name', 'item_category',)}

So, How can I get category_name in that slug instead of pk as explained first?

Below is my model
class ItemPropertyMaster(models.Model):
    property_name
= models.CharField(max_length=500)
    property_slug
= models.SlugField(max_length=500, unique=True)
    item_category
= models.ForeignKey(ItemCategory, on_delete=models.CASCADE)
    created_date
= models.DateTimeField(auto_now_add=True)


   
class Meta:
       
'''same property can not be in one category'''


        unique_together
= (
           
("property_name", "item_category"),
       
)


   
def __str__(self):
       
return self.property_name


~ Thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b2458761-3e7b-4152-ba91-6fa3f49ea94b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment