I’m going to use ‘prepopulated_fields’ at Django admin to get a YouTube embed code from its video id, as below.
class VideoAdmin(admin.ModelAdmin):
prepopulated_fields = {"embed_code": ("embed_id",)}
inlines = (CourseVideoInline,)
but embed_code I can get from “Rmp6zIr5y4U” is “rmp6zir5y4u”, all capital letters changed into small ones.
Have any ideas to solve this?
or is any better ways to get embed code from video id by customizing Django admin?
Thanks!
Video model (related fields) is below.
class Video(models.Model):
embed_code = models.TextField(max_length=1000)
embed_id = models.CharField(max_length=200)
prepopulated_fieldsare mainly used for generating slugs from titles via javascript – hence why you see it in lowercase. You might be better off overwriting the models save function so that when an instance of your model is saved, it can grab the video_id and generate the embed_code:an alternative is to do this at the admin view level (as opposed to the model level). You could do this by overwriting the
save_modelmethod of theModelAdminclass. This gives you the added bonus of having access to the request and the form:This code is untested, it’s just to illustrate the two places you might want to achieve what you are looking to do