开发者

how to define a widget in a model attribute

开发者 https://www.devze.com 2022-12-08 09:04 出处:网络
Simply, I write: # forms.py class NoteForm(ModelForm): def __init__(self, *args, **kwargs): super(NoteForm, self).__init__(*args, **kwargs)

Simply, I write:

# forms.py
class NoteForm(ModelForm):
def __init__(self, *args, **kwargs): 
    super(NoteForm, self).__init__(*args, **kwargs)
    #add attributes to html-field-tag:
    self.fields['content'].widget.attrs['rows'] = 3
    self.fields['title'].widget.attrs['size'] = 20

class 开发者_如何学编程Meta:
    model = Note
    fields = ('title','content')

To add or modify some attributes to the HTML tag field.

But in this time, I will have to do it directly in the Note model in models.py

#models.py
class Note(models.Model):
    title =   models.CharField(max_length=100)
    content = models.TextField(max_length=255)
    #how to define the fields.widget.attrs here ?????

Just FYI: Why? Because is going to work as a

NotesFormSet = inlineformset_factory(NoteBook, Note, extra=10)

To add many Notes when adding/editing a NoteBook

NOTE: It is not django admin

If im not giving you sufficient context, comment plz, any hint is apreciated =')


No, you still do it in a form, but you just pass that form as a parameter to inlineformset_factory.

NotesFormSet = inlineformset_factory(NoteBook, Note, extra=10, form=NoteForm)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号