I'm trying to use CheckboxSelectMultiple in a form, instead of the SelectMultiple default. I tried two different ways to make it display as checkboxes, but they both display the default SelectMultiple on my local webpage. Everything else renders correctly ( {{ form.as_table }} ). Would someone point me in the right direction? Thanks,
Django version 3.0.2
Python 3.6.9
models.py
class MyModel(models.Model):
m2m = models.ManyToManyField('OtherModel', blank=True)
[...]
forms.py
from django.forms import ModelForm
from myapp.models import MyModel
class MyModelModelForm(ModelForm):
[...]
# I tried this...
m2m = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple)
class Meta:
model = MyModel
fields = '__all__'
# or this...
widgets = {
'm2m': forms.CheckboxSelectMultiple,
}
views.py
from django.views.generic.edit import CreateView
from myapp.models import MyModel
class MyModelCreate(CreateView):
model = MyModel
fields = '__all__'
template_name = 'MyApp/mymodel_form.html'
You did not use the form you constructed. You simply constructed a ModelForm, and then let the CreateView create another ModelForm since you did not specify a form_class [Django-doc].
You can thus update the CreateView, and work with:
from django.views.generic.edit import CreateView
from myapp.models import MyModel
from myapp.forms import MyModelModelForm
class MyModelCreate(CreateView):
model = MyModel
form_class = MyModelModelForm
template_name = 'MyApp/mymodel_form.html'
Related
I'm making a user login form with a CustomUser model derived from AbstractUser, with one extra field: date_of_birth. I use CreateView to generate the form. All fields show up, the password field uses the password widget as expected (showing dots instead of characters), but the date field does not (plain character field with no formatting or calendar). What am I overlooking?
models.py:
from django.urls import reverse
from django.contrib.auth.models import AbstractUser
# Create your models here.
class CustomUser(AbstractUser):
date_of_birth = models.DateField(verbose_name="Date of Birth", blank=True, null=True)
def get_absolute_url(self):
return reverse('index')
def __str__(self):
return self.username
forms.py:
from .models import CustomUser
class CustomUserForm(forms.ModelForm):
class Meta:
model = CustomUser
fields = ["username", "password", "first_name", "last_name", "email", "date_of_birth"]
widgets = {
"password": forms.PasswordInput(),
"date_of_birth": forms.DateInput()
}
views.py:
from django.views.generic.edit import CreateView
from .models import CustomUser
from .forms import CustomUserForm
# Create your views here.
def index(request):
return HttpResponse("Hello, world")
class CustomUserCreate(CreateView):
model = CustomUser
form_class = CustomUserForm
If you come here in 2020 and beyond, just overide the default type=text undelying input by using 'type':'date'
So something like the below would work. Tested on Mozilla Dev Edition 73.+
'date_of_birth': forms.DateInput(attrs={'class':'form-control', 'type':'date'}),
Django has no built-in fancy datepicker. DateField uses the DateInput widget which is just a text input.
Thanks voodoo-burger for pointing me in the right direction. I found a video with a very simple solution to use the HTML5 datepicker: https://www.youtube.com/watch?v=I2-JYxnSiB0.
It only requires to add the following to forms.py:
class DateInput(forms.DateInput):
input_type = 'date'
and then use this as the widget (so replace forms.DateInput() with DateInput()).
I'm trying to use autocomplete_light and taggit both on an admin form.
I've read the docs on integrating autocomplete light and taggit here,
and the docs on integrating autocomplete light in the admin here. But there seems to be little (or no) discussion on doing both at the same time.
what I've got so far.
In models.py:
from django.db import models
from taggit.managers import TaggableManager
from taggit.models import TagBase, GenericTaggedItemBase
class MyTag(TagBase):
description = models.CharField(max_length = 250, blank = True, null = True)
class MyTagThroughModel(GenericTaggedItemBase):
tag = models.ForeignKey(MyTag, related_name = "tagged_items")
class MyModel(models.Model):
Name = models.CharField(max_length = 200)
...
tags = TaggableManager(through = MyTagThroughModel)
In autocomplete_light_registry.py:
import autocomplete_light
from models import MyTag
autocomplete_light.register(MyTag)
How am I meant to structure admin.py?
If this was a non-admin form, the field would be given as:
tags = TagField(widget = TagWidget('MyTagAutocomplete'))
If this was a non-taggit admin form, I would add the following to the admin model class:
form = autocomplete_light.modelform_factory(MyTag)
How can I combine the two?
How am I meant to structure admin.py?
Here's an example to autocomplete Tags. It shows you how autocomplete_light and taggit work on admin and non-admin forms.
models.py
from django.db import models
from taggit.managers import TaggableManager
class MyModel(models.Model):
name = models.CharField(max_length = 200)
tags = TaggableManager(blank=True)
autocomplete_light_registry.py
import autocomplete_light
from taggit.models import Tag
autocomplete_light.register(Tag)
forms.py
from django import forms
import autocomplete_light
from autocomplete_light.contrib import taggit_tagfield
from models import MyModel
class MyModelForm(forms.ModelForm):
tags = taggit_tagfield.TagField(widget=taggit_tagfield.TagWidget('TagAutocomplete'))
class Meta:
model = MyModel
widgets = {
'tags': autocomplete_light.TextWidget('TagAutocomplete'),
}
admin.py
from django.contrib import admin
import autocomplete_light
from models import MyModel
from forms import MyModelForm
class MyModelAdmin(admin.ModelAdmin):
form = MyModelForm
model = MyModel
admin.site.register(MyModel, MyModelAdmin)
views.py
from django.views.generic.edit import CreateView
from models import MyModel
from forms import MyModelForm
class CreateMyModel(CreateView):
model = MyModel
form_class = MyModelForm
urls.py
from django.conf.urls import patterns, url
from views import CreateMyModel
urlpatterns = patterns('',
url(r'^create/$', CreateMyModel.as_view()),
)
The quick docs seem to be more straightforward to understand than the docs you were looking at.
Consider using django-taggit-autosuggest instead.
It works best with the django-grapelli admin skin.
is there any way to show only a list of fields or excluding some of them when using django-rest-framework?
Here's my app/views.py:
from rest_framework.generics import ListAPIView
from .models import PhpbbUsers
class UsersReadView(ListAPIView):
model = PhpbbUsers
Obiously there are some user information that I don't want to show to everyone. How could I do?
Solution code
from rest_framework import generics, serializers
from .models import PhpbbUsers
class UsersSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = PhpbbUsers
fields = ('user_id', 'username', 'user_avatar')
class UsersReadView(generics.ListAPIView):
model = PhpbbUsers
serializer_class = UsersSerializer
Set the serializer_class attribute on the view.
See the quickstart for a good example: http://django-rest-framework.org/tutorial/quickstart.html
I want to create a view that is able to show a ModelForm for various different models. It does this by obtaining the content type of the model and then dynamically instantiating the model form associated with that particular model. Here is my model:
from django.db import models
class SomeModel(models.Model):
name = models.CharField(max_length=150)
def __unicode__(self):
return self.name
And inside the same app there is a forms.py with the following form:
from django.forms import ModelForm
from someapp.models import SomeModel
class SomeModelForm(ModelForm):
class Meta:
model = SomeModel
fields = ('name',)
So what I want to do inside of my view file is return the correct form for each model dynamically. I tried the following:
from django.db import models
from someapp.forms import SomeModelForm
class SomeModel(models.Model):
name = models.CharField(max_length=150)
form = SomeModelForm
def __unicode__(self):
return self.name
But it doesn't work because of the obvious circular import. Does anyone have any idea how I might go about achieving this? I tried toying with modelform_factory, but it seems to ignore any of my custom model forms in forms.py.
EDIT: I should of mentioned that I won't have an instance of the model, just the model class itself, so having a method that inside of the model doesn't work (it does, however, work if you are calling it on an instance of the model)
You could get around the circular import by importing your model form inside a method.
class SomeModel(models.Model):
name = models.CharField(max_length=150)
#staticmethod
def get_form_class():
from someapp.forms import SomeModelForm
return SomeModelForm
# in your view:
SomeModel.get_form_class()
Putting the import within a method on the model should be enough to get you around the circular import, so instead of what you have, you'd use:
class SomeModel(models.Model):
...
def get_form(self):
from someapp.forms import SomeModelForm
return SomeModelForm
You can even make it a property if you want with:
form = property(get_form)
There is a built-in func get_model for lazy importing models.
from django.db.models import get_model
SomeModel = get_model('your_app_name', 'SomeModel')
Using __import__ and getattr.
# models.py
class SomeModel(models.Model):
...
#classmethod
def get_form(cls):
try:
app = __import__(cls._meta.app_label)
forms = getattr(app, "forms")
return getattr(forms, "%sForm" % cls.__name__)
except:
return None
# forms.py
class SomeModelForm(forms.Form):
...
in a view you can get the form associate to a models like this:
# views.p
from models import SomeModel
...
def myview(request):
form = SomeModel.getform()
Is it possible to make django's (v1.2) URLField output an HTML5 input tag where type="url"?
------------- SOLUTION -------------
from django.forms import ModelForm
from django.forms import widgets
from django.forms import fields
from models import MyObj
class URLInput(widgets.Input):
input_type = 'url'
class MyObjForm(ModelForm):
url = fields.URLField(widget=URLInput())
class Meta:
model = MyObj
You have to create a custom widget for that.
class URLInput(forms.TextInput):
input_type = 'url'
Then you can pass this widget to URLField constructor:
class MyForm(forms.Form):
url = forms.URLField(widget=URLInput())