I have a models.py:
class MyModel(models.Model):
...
fries_with_that = models.BooleanField()
forms.py:
class MyModelForm(ModelForm):
class Meta:
model = MyModel
fields = (
'fries_with_that',
)
This works fine, and gives me drop-down with 'unknown', 'yes' and 'no' as choices. But I really want a checkbox. So I added:
Edit
def __init__(self, *args, **kwargs):
super(MyModelForm, self).__init__(*args, **kwargs)
self.fields['fries_with_that'].widget = forms.CheckboxInput()
Now I get a checkbox but when checked, the form returns a value of None. Why is this?
Edit
views.py:
form = MyModelForm(request.POST or None)
if form.is_valid():
# UPDATE PROJECT
updated = MyModel.objects.filter(
project_id=project_id
).update(**form.cleaned_data)
if updated == 0:
project = form.save()
Try this:
class MyModelForm(ModelForm):
fries_with_that = forms.BooleanField(widget=forms.CheckboxInput, default=False)
class Meta:
model = MyModel
fields = (
'fries_with_that',
)
actually, this should render the checkbox.
fries_with_that = forms.BooleanField()
Related
Currently, when a user creates a task, they can assign it to all users. I only want them to be able to assign a task based on the members of the project. I feel like the concept I have right now works but I need to replace the ????. Task's assignee has a foreignkey relationship with the user_model. The user_model is also connected with members on a many to many relationship.
projects/models.py
class Project(models.Model):
name = models.CharField(max_length=200)
description = models.TextField()
members = models.ManyToManyField(USER_MODEL, related_name="projects")
tasks/models.py
class Task(models.Model):
name = models.CharField(max_length=200)
start_date = models.DateTimeField()
due_date = models.DateTimeField()
is_completed = models.BooleanField(default=False)
project = models.ForeignKey(
"projects.Project", related_name="tasks", on_delete=models.CASCADE
)
assignee = models.ForeignKey(
USER_MODEL, null=True, related_name="tasks", on_delete=models.SET_NULL
)
tasks/views.py
class TaskCreateView(LoginRequiredMixin, CreateView):
model = Task
template_name = "tasks/create.html"
# fields = ["name", "start_date", "due_date", "project", "assignee"]
form_class = TaskForm
def get_form_kwargs(self):
kwargs = super(TaskCreateView, self).get_form_kwargs()
kwargs["user"] = self.request.user
kwargs["project_members"] = ??????????
return kwargs
tasks/forms.py
class TaskForm(ModelForm):
class Meta:
model = Task
fields = ["name", "start_date", "due_date", "project", "assignee"]
def __init__(self, *args, **kwargs):
user = kwargs.pop("user")
project_members = kwargs.pop("project_members")
super(TaskForm, self).__init__(*args, **kwargs)
self.fields["project"].queryset = Project.objects.filter(members=user)
self.fields["assignee"].queryset = Project.objects.filter(
members=?????????
)
Update:
I followed SamSparx's suggestions and changed the URL paths so now TaskCreateView knows which project id. I updated my tasks/views to the following but I get a TypeError: "super(type, obj): obj must be an instance or subtype of type" and it points to the line: form = super(TaskForm, self).get_form(*args, **kwargs) Maybe it has something to do with having a get_form_kwargs and get_form function? I kept my existing features for the custom form such as when a user creates a task, they can only select projects they are associated with.
Views.py updated
class TaskCreateView(LoginRequiredMixin, CreateView):
model = Task
template_name = "tasks/create.html"
form_class = TaskForm
def get_form_kwargs(self):
kwargs = super(TaskCreateView, self).get_form_kwargs()
kwargs["user"] = self.request.user
return kwargs
def get_form(self, *args, **kwargs):
form = super(TaskForm, self).get_form(*args, **kwargs)
form.fields["assignee"].queryset = Project.members.filter(
project_id=self.kwargs["project_id"]
)
def form_valid(self, form):
form.instance.project_id = Project.self.kwargs["project_id"]
return super(TaskCreateView, self).form_valid(form)
def get_success_url(self):
return reverse_lazy("list_projects")
I have also tried to update the forms.py with the following but get an error that .filter cannot be used on Many to Many relationships.
Updated forms.py
class TaskForm(ModelForm):
class Meta:
model = Task
fields = ["name", "start_date", "due_date", "project", "assignee"]
def __init__(self, *args, **kwargs):
user = kwargs.pop("user")
super(TaskForm, self).__init__(*args, **kwargs)
self.fields["project"].queryset = Project.objects.filter(members=user)
self.fields["assignee"].queryset = Project.members.filter(
project_id=self.kwargs["project_id"]
)
Another thing I have tried is to go back to my first approach now that I have the url paths: tasks/create/(project_id)
Views.py
class TaskCreateView(LoginRequiredMixin, CreateView):
model = Task
template_name = "tasks/create.html"
form_class = TaskForm
def get_form_kwargs(self):
kwargs = super(TaskCreateView, self).get_form_kwargs()
kwargs["user"] = self.request.user
kwargs["project_id"] = Project.objects.all()[0].members.name
# prints to auth.User.none
return kwargs
I feel like if the kwargs["project_id"] line can be changed to getting list of members of whatever project with the ID in the URL, then this should solve it
Forms.py
class TaskForm(ModelForm):
class Meta:
model = Task
fields = ["name", "start_date", "due_date", "project", "assignee"]
def __init__(self, *args, **kwargs):
user = kwargs.pop("user")
project_id = kwargs.pop("project_id")
super(TaskForm, self).__init__(*args, **kwargs)
self.fields["project"].queryset = Project.objects.filter(members=user)
self.fields["assignee"].queryset = Project.objects.filter(
members=project_id
)
The problem here is that your task doesn't know what members are relevant to include as assignees until you have chosen the project the task belongs to, and both project and assignee are chosen in the same form, so Django doeesn't know who is relevant yet.
The easiest way to handle this is to ensure the call to create a task is associated with the project it is going to be for - eg,
Update your URLs to handle the project ID
Path('create-task/<int:project_id>', TaskCreateView.as_view(), name='create_task')
Update your view
class TaskCreateView(LoginRequiredMixin, CreateView):
model = Task
template_name = "tasks/create.html"
# fields = ["name", "start_date", "due_date", "assignee"]
#NB: I have remove project from the field list, you may need to do the same in your form as it is handled elsewhere
form_class = TaskForm
def get_form(self, *args, **kwargs):
form = super(TaskCreateView, self).get_form(*args, **kwargs)
form.fields['assignee'].queryset = Project.members.filter(project_id = self.kwargs['project_id'])
Return form
def form_valid(self, form):
form.instance.project_id = project.self.kwargs['project_id']
return super(TaskCreateView, self).form_valid(form)
Add links
Create Task for this project
This will create a link on the project details page, or underneath the project in a listview to 'create task for this project', carrying the project informaton for the view via the URL. Otherwise you will have to get into some rather more complex ajax calls that populate the potential assignees list based on the selection within the project dropdown in a dynamic fashion
How do I limit the values returned via the ManyToMany relationship and thus displayed in the <SELECT> field on my form to only show the spots which were created by the currently logged in user?
models.py
class Project(models.Model):
owner = models.ForeignKey(User, editable=False)
...
spots = models.ManyToManyField(to='Spot', blank=True, )
class Spot(models.Model):
owner = models.ForeignKey(User, editable=False)
spot_name = models.CharField(max_length=80, blank=False)
forms.py
from django import forms
from .models import Project, Spot
class ProjectForm(forms.ModelForm):
class Meta:
model = Project
exclude = ('owner', )
class SpotForm(forms.ModelForm):
class Meta:
model = Spot
exclude = ('owner', )
I'm using GenericViews for Update and Create and currently see all of the entries everyone has made into Spots when I'm updating or creating a Project. I want to see only the entries entered by the logged in user. For completeness sake, yes, the project.owner and spot.owner were set to User when they were created.
I've tried def INIT in the forms.py and using limit_choices_to on the manytomany field in the model. Either I did those both wrong or that's not the right way to do it.
thank you!
in your forms.py
class ProjectForm(forms.ModelForm):
class Meta:
model = Project
exclude = ('owner', )
def __init__(self, user_id, *args, **kwargs):
self.fields['spots'] = forms.ModelChoiceField(widget=forms.Select, queryset=Project.objects.filter(owner=user_id))
class SpotForm(forms.ModelForm):
class Meta:
model = Spot
exclude = ('owner', )
def __init__(self, user_id, *args, **kwargs):
self.fields['spot_name'] = forms.ModelChoiceField(widget=forms.Select, queryset=Spot.objects.filter(owner=user_id))
in your views.py
user_id = Project.objects.get(owner=request.user).owner
project_form = ProjectForm(user_id)
spot_form = SpotForm(user_id)
As I mentioned above, Dean's answer was really close, but didn't work for me. Primarily because request is not accessible in the view directly. Maybe it is in older Django versions? I'm on 1.9. Thank you Dean, you got me over the hump!
The gist of what's going on is adding User into the kwargs in the View, passing that to the ModelForm, remove User from the kwargs and use it to filter the Spots before the form is shown.
This is the code that worked for my project:
views.py
class ProjectUpdate(UpdateView):
model = Project
success_url = reverse_lazy('projects-mine')
form_class = ProjectForm
def dispatch(self, *args, **kwargs):
return super(ProjectUpdate, self).dispatch(*args, **kwargs)
def get_form_kwargs(self):
kwargs = super(ProjectUpdate, self).get_form_kwargs()
kwargs.update({'user': self.request.user})
return kwargs
forms.py
class ProjectForm(forms.ModelForm):
class Meta:
model = Project
exclude = ('owner', 'whispir_id')
def __init__(self, *args, **kwargs):
user_id = kwargs.pop('user')
super(ProjectForm, self).__init__(*args, **kwargs)
self.fields['spots'] = forms.ModelMultipleChoiceField(queryset=Spot.objects.filter(owner=user_id))
class SpotForm(forms.ModelForm):
class Meta:
model = Spot
exclude = ('owner', )
def __init__(self, *args, **kwargs):
user_id = kwargs.pop('user')
super(SpotForm, self).__init__(*args, **kwargs)
self.fields['spot_name'] = forms.ModelMultipleChoiceField(queryset=Spot.objects.filter(owner=user_id))
Given the django model below, is it possible to automatically update a subset of the fields to be RadioSelect instead of the default Select?
class ExampleModel(models.Model):
field1_radios = models.CharField(max_length=1, choices=DEFAULT_CHOICES)
field2_radios = models.CharField(max_length=1, choices=DEFAULT_CHOICES)
field3_radios = models.CharField(max_length=1, choices=DEFAULT_CHOICES)
field4_radios = models.CharField(max_length=1, choices=DEFAULT_CHOICES)
field5 = models.CharField(max_length=50)
...
I can achieve this manually with:
class ExampleForm(forms.ModelForm):
class Meta:
model = ExampleModel
widgets = {
'field1_radios': forms.RadioSelect(),
'field2_radios': forms.RadioSelect(),
'field3_radios': forms.RadioSelect(),
'field4_radios': forms.RadioSelect()}
When I try to automate the creation of the widgets the choices are lost (and the radio buttons are not rendered):
def custom_callback(f, *args, **kwargs):
if f.name.endswith('_radios'):
formfield = f.formfield()
formfield.widget = forms.RadioSelect()
return formfield
else:
return f.formfield()
class ExampleForm(forms.ModelForm):
formfield_callback = custom_callback
class Meta:
model = ExampleModel
ahh, it looks like I was missing the final step of re-specifying the original choices, this is not done automatically:
def custom_callback(f, *args, **kwargs):
if f.name.endswith('_radios'):
formfield = f.formfield()
formfield.widget = forms.RadioSelect(choices=formfield.choices)
return formfield
else:
return f.formfield()
I haven't seen formfield_callback used very often, and I don't think it's documented.
Another approach would be to override formfield_for_choice_field.
class MyModelAdmin(admin.ModelAdmin):
...
def formfield_for_choice_field(self, db_field, request, **kwargs):
"""Use a radio select instead of a select box"""
kwargs['widget'] = forms.RadioSelect
if 'choices' not in kwargs:
# this was required to prevent displaying the empty label ---------
kwargs['choices'] = db_field.get_choices(include_blank=False)
return super(MyModelAdmin, self).formfield_for_choice_field(db_field, request, **kwargs)
my model:
class Event(models.Model):
title = models.CharField(max_length=255)
start = models.DateTimeField()
end = models.DateTimeField()
theme = models.ForeignKey(Theme)
class Theme(models.Model):
name = models.CharField(max_length=100)
color = models.CharField(max_length=50)
text_color = models.CharField(max_length=50)
my form:
class EventForm(ModelForm):
class Meta:
model = Event
fields = ['title', 'start', 'end']
theme = forms.ModelChoiceField(
queryset=Theme.objects.filter(public=True),
empty_label='None'
)
my view:
#login_required
def index(request):
if request.method == 'POST':
form = EventForm(request.POST)
if form.is_valid():
form.save()
Now If I fill in the values in the form star, end, title and select a theme from a list that django creates for me I get an error when I try to run the form.save() method.
IntegrityError: null value in column "theme_id" violates not-null constraint
But when I look into form.cleaned_data I can see that in theme is an instance of my Theme model available.
you cannot save Event without Theme object, so you need something like
form = EventForm(request.POST)
if form.is_valid():
# get your Theme object 'your_theme_object'
event = form.save(commit=False)
event.theme = your_theme_object
event.save()
I should have commented but I don't have enough point.
I think better way to achieve this thing is:
class EventForm(ModelForm):
class Meta:
model = Event
fields = ['title', 'start', 'end', 'theme']
As 'theme' is foreign key to Event Model, it'll appear as drop down on your template.
As here you want to filter theme objects, you can achieve it by overriding init :
class EventForm(ModelForm):
def __init__(self, *args, **kwargs):
super(EventForm, self).__init__(*args, **kwargs)
self.fields['theme'].queryset = self.fields['theme'].queryset.filter(public=True)
class Meta:
model = Event
fields = ['title', 'start', 'end', 'theme']
I have a model like this:
class News(models.Model):
is_activity = models.BooleanField(default=False)
activity_name = models.CharField(max_length=240, blank=True, null=True)
What I am trying to achieve is, if is_activity is checked in I want activity_name to be required. Thus, I am trying to override the __init__ method:
class NewsForm(forms.ModelForm):
class Meta:
model = News
def __init__(self, *args, **kwargs):
super(NewsForm, self).__init__(*args, **kwargs)
if self.fields['is_activity'] is True:
self.fields['activity_name'].required = True
class NewsAdmin(FrontendEditableAdminMixin, admin.ModelAdmin):
form = NewsForm
Even if I check in the is_activity the activity_name is non-required. What's wrong?
The ModelForm.clean() method gives you access to the cleaned data – this is where you can include the field-specific conditional logic:
from django.core.validators import EMPTY_VALUES
class NewsForm(forms.ModelForm):
class Meta:
model = News
def clean(self):
is_activity = self.cleaned_data.get('is_activity', False)
if is_activity:
# validate the activity name
activity_name = self.cleaned_data.get('activity_name', None)
if activity_name in EMPTY_VALUES:
self._errors['activity_name'] = self.error_class([
'Activity message required here'])
return self.cleaned_data
class NewsAdmin(FrontendEditableAdminMixin, admin.ModelAdmin):
form = NewsForm