Answer the question
In order to leave comments, you need to log in
How to pass object id to its field in django in django?
Actually there is a news model, installed ckeditor (this is not very important, you can imagine a regular field, but with a handler), put it as a field in the model. When opening in the admin panel, the id of this news should be passed in the ckeditor field.
from django.db import models
from ckeditor_uploader.fields import RichTextUploadingField
# вот примерно как сейчас
class Page( models.Model ):
title = models.CharField( verbose_name="Заголовок")
content = RichTextUploadingField( verbose_name="Текст" )
# вот как хотелось бы, но так не выйдет, т.к. объекта еще нет, всего лишь описывается класс
class Page( models.Model ):
title = models.CharField( verbose_name="Заголовок")
content = RichTextUploadingField( verbose_name="Текст", page_id=self.id )
class Page( models.Model ):
title = models.CharField( verbose_name="Заголовок")
content = RichTextUploadingField( verbose_name="Текст" )
def __init__(self, *args, **kwargs):
#print(self.id) # AttributeError: 'Page' object has no attribute 'id'
super(Page, self).__init__(*args, **kwargs)
print(self.id) # выведет ид
Answer the question
In order to leave comments, you need to log in
Try passing the id through the form widget when initializing
admin.py
from forms import PageForm
@admin.register(Page)
class PageAdmin(admin.ModelAdmin)
form = PageForm
model = Page
class PageForm(forms.ModelForm)
def __init__(self, *args, **kwargs):
super(PageForm, self).__init__(*args, **kwargs)
if self.instance:
self.fields['content'].widget=RichTextUploadingField( verbose_name="Текст", page_id=self.instance.id )
...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question