How can i make a queryset in this modelform. This is my code.
Class Sample(forms.ModelForm):
class Meta:
model = Customer
fields = ('name','address',)
widgets = {
'name' : Select(attrs={'class':'span2'}),
'address' : TextInput(attrs={'class':'span4'}),
}
queryset = {'name': User.objects.filter(type_id=1)}
Is this the right way in using queryset? Pls help me.
Thank You.
Class Sample(forms.ModelForm):
class Meta:
model = Customer
fields = ('name','address',)
widgets = {
'name' : Select(attrs={'class':'span2'}),
'address' : TextInput(attrs={'class':'span4'}),
}
def __init__(self, *args, **kwargs):
super(Sample, self).__init__(*args, **kwargs)
self.fields['name'].queryset = User.objects.filter(type_id=1)
Related
How can I delete label of form in Django. I have something like this.
class ProfileUpdateForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['user_image']
widgets = {
'user_image': forms.FileInput(attrs={'class': 'image-upload', 'label': ''}),
}
image-upload class does not include label. I have a auto-generated label and is written 'user-image'
You can remove it via init:
class ProfileUpdateForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['user_image']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['user_image'].label = ''
For instance, I have this code. How can I add the empty_label to the field Select.
class NameForm(forms.ModelForm):
class Meta:
model = Model
fields = ['choice',]
widgets = {
'choice': forms.Select(attrs={'class': 'class'}, ??empty_label='lorem'??),
}
models.py
class Book(models.Model):
choice = models.ForeignKey('Another Model', on_delete=models.PROTECT, null=True)
This is a parameter of the form field, not the widget. If you do not want to override the rest of the form field, you specify this in the constructor of the form:
class NameForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['choice'].empty_label = 'lorem'
class Meta:
model = Model
fields = ['choice',]
widgets = {
'choice': forms.Select(attrs={'class': 'class'}),
}
I have a table like that:
import django_tables2 as tables
from .models import MyModel
class MyTable(tables.Table):
class Meta:
model = MyModel
fields = ['myfield', 'relatedtable.otherfield']
Since I can not have render_relatedtable.otherfield and render_relatedtable__otherfield does not work, how can I override render_<column_name> or value_<colum_name> for relatedtable.otherfield? Is it even possible?
I tried following approaches, but none of them worked:
Override attributes in __init__()
class MyTable(tables.Table):
class Meta:
model = MyModel
fields = ['myfield', 'relatedtable.otherfield']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.columns['relatedtable.otherfield'].render = myrenderfnc
# and/or
self.columns['relatedtable.otherfield'].column.render = myrenderfnc
Custom column
class MyColumn(tables.Column):
def render(self, record):
return getattr(record, 'relatedtable.otherfield')
class MyTable(tables.Table):
class Meta:
model = MyModel
fields = ['myfield']
otherfield = MyColumn()
'Renaming' column
class MyTable(tables.Table):
class Meta:
model = MyModel
fields = ['myfield']
def __init__(self, *args, **kwargs):
exclude = ['relatedtable.otherfield']
extra_columns = [('otherfield', self.base_columns['relatedtable.otherfield')]
super().__init__(*args, exclude=exclude, extra_columns=extra_columns, **kwargs)
self.columns['relatedtable.otherfield'].render = myrenderfnc
# and/or
self.columns['relatedtable.otherfield'].column.render = myrenderfnc
One way to do it is explicitly define the column with an accessor, and then use the column name in your render_FOO method name like this:
class MyTable(tables.Table):
otherfield = MyColumn(accessor='relatedtable.otherfield')
class Meta:
model = MyModel
fields = ['myfield']
def render_otherfield(self, record, value):
return value
Your 'custom column' example example should also work though.
How do I pass in a value to an attribute constructor in a django form?
In other words, I have a form like so, and I'd like to set SOMETHING when I instantiate the form.
class ImageUploadFileForm(forms.ModelForm):
class Meta:
model = Photo
fields = [ 'image' ]
image = cloudinary.forms.CloudinaryJsFileField({ 'public_id': SOMETHING })
i.e. in the view:
uploadform = ImageUploadFileForm(instance=whatever, something='BLAHBLAHBLAH')
I have the suspicion that I'm thinking about this wrongly...
Thx Shang Wang!
For all of you searching in CloudinaryJSFileField, don't forget to add 'options', like so:
class ImageUploadFileForm(ModelFormControlMixin):
class Meta:
model = Photo
fields = [ 'image' ]
def __init__(self, *args, **kwargs):
self.public_id = kwargs.pop('public_id')
super(ImageUploadFileForm, self).__init__(*args, **kwargs)
self.fields['image'] = cloudinary.forms.CloudinaryJsFileField(options={ 'public_id': str(self.public_id) })
class ImageUploadFileForm(forms.ModelForm):
class Meta:
model = Photo
fields = [ 'image' ]
def __init__(self, *args, **kwargs):
self.something = kwargs.pop('something')
super(ImageUploadFileForm, self).__init__(*args, **kwargs)
self.fields['image'] = cloudinary.forms.CloudinaryJsFileField({ 'public_id': self.something })
Then
uploadform = ImageUploadFileForm(instance=whatever, something='BLAHBLAHBLAH')
Pretty standard way of passing arguments to form constructor.
I know how to add a 'class' or other widget attribute to an automatically built ModelForm:
class ExampleSettingForm(ModelForm):
def __init__(self, *args, **kwargs):
super(ExampleSettingForm, self).__init__(*args, **kwargs)
self.fields['example_field'].widget.attrs['class'] = 'css_class'
class Meta:
model = Example
How do I insert a help_text= into the example_field Field?
As of Django 1.6: You can edit it within the Meta class. Try:
class ExampleSettingForm(ModelForm):
def __init__(self, *args, **kwargs):
super(ExampleSettingForm, self).__init__(*args, **kwargs)
self.fields['example_field'].widget.attrs['class'] = 'css_class'
class Meta:
model = Example
help_texts = {
'example_field': ('Here is some help'),
}
Docs on this are at https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overriding-the-default-fields. See release notes at http://django.readthedocs.org/en/latest/releases/1.6.html . You can set your own label, help_text and error_messages.
This is what I did in Django 1.9:
class MyModelForm(forms.ModelForm):
class Meta:
model = MyModel
fields = ('__all__')
help_texts = {
"my_field": "This is case sensitive..."
}