I'm using django form to add custom field in my form, but I want to exclude this custom field in some condition(when user_id is set on url query param), how can I handle it?
Something interesting happens in my try and error, I put my custom field name in Mata.exclude of my form, but this custom field still generates in my form !!!!
Here is my code:
class PushTokenForm(forms.ModelForm):
push_scenario = forms.ChoiceField(
label=_('Push Scenario'),
choices=get_scenarios(),
)
class Meta:
model = PushToken
exclude = ['failed', 'push_scenario']
UPDATE
def get_form(self, request, obj=None, **kwargs):
self.exclude = []
user_id = request.GET['user_id'] if 'user_id' in request.GET else None
if user_id:
self.exclude.append('push_scenario')
form = super(PushTokenAdmin, self).get_form(request, obj, **kwargs)
return form
Related
this is my views.py :
i want save type in device field in model
class GetDeviceMixin( object):
def setup(self, request, *args, **kwargs):
super().setup( request, *args, **kwargs)
type= request.META['HTTP_USER_AGENT']
print(type)
return type
class RegisterView(GetDeviceMixin , generic.CreateView):
form_class = CustomUserCreationForm
success_url = reverse_lazy("register")
template_name = "man/man.html"
and this is my models.py
class account(AbstractBaseUser):
first_name= models.CharField(max_length=20,verbose_name="first name")
device = models.CharField(verbose_name="device" , max_length=100)
this is my forms.py:
class GetReq(forms.ModelForm):
class Meta:
model = account
fields = ['device',]
First, pop Classy CBVs onto your browser bookmark list ...
I'm assuming that you want to use a form, either to get other information from the user, or to allow the user to override the automatically determined value of device. In this case, you want to pass it as the initial value for device to the form
Now, look at CreateView to work out what to subclass. get_initial() looks hopeful, so
def get_initial(self):
initial = super().get_initial()
initial['device'] = self.device_type # as per the comment!
return initial
You should now see a form with the automatically determined value as the default value
If the intent was to get other fields of the model from the user and to always forcibly insert the automatically determined device_type, you would instead subclass form_valid
def form_valid(form):
obj = form.save( commit=False)
obj.device = self.device_type
obj.save()
I want to use UpdateView in my model Event. This model had this field:
employee = models.ForeignKey(User, on_delete=models.CASCADE, related_name='event_employee')
My view :
class UpdateEvent(UpdateView):
model = Event
template_name = 'dashboard/pro_update_event.html'
form_class = UpdateEventForm
other_variable = None
def get_form_kwargs(self):
kwargs = super(UpdateEvent, self).get_form_kwargs()
names_clients = User.objects.filter(professionnels=self.request.user)
kwargs.update({'names_clients': names_clients})
return kwargs
def get_success_url(self, *args, **kwargs):
return reverse_lazy('pro_details_event', kwargs={'pk': self.object.pk})
My Form :
class UpdateEventForm(forms.ModelForm):
"""
edit an event
"""
class Meta():
model = Event
fields = ('employee', 'date_start', 'date_end')
def __init__(self, names_clients, *args, **kwargs):
super(UpdateEventForm, self).__init__(*args, **kwargs)
self.fields['employee'] = forms.ChoiceField(choices=tuple([(client.pk,client.last_name.capitalize()+" "+client.first_name.capitalize()) for client in names_clients]))
It seems work, the widget "select" contain the correct values.
example : <option value="2">Dupond Jean</option>
But when I submit the form :
Cannot assign "'2'": "Event.employee" must be a "User" instance.
I don't understand because if remove "get_form_kwargs" in my view and "def init" in my form, the value passed is the same (the pk of the employee). It's works with this way.
But the problem is all employee are selectable and the username is display not the firstname and lastname.
It's because the employee ForeignKey on your Event model points to the User model, but you're populating the choice field for the employee field with Particulier model data.
Either change the foreign key to point to the Particulier model, or pass in Users to the form kwargs
Sorry, I forgot to fix this error in the code in this page.
But I have the error in my 1st comment, with populating the choice field with an User model data.
I have a simple CategoryForm which has a hidden field that automatically gets added during save on the front-end. In the Admin panel I would like is_staff users to be able to add a Category while the field is hidden there as well. To superusers I would like the field to be shown. How do I get the excluded fields back in my Admin form?
Forms.py:
class CategoryForm(forms.ModelForm):
class Meta:
model = Category
fields = ('category', 'company',)
exclude = ['company']
widgets = {'company': forms.HiddenInput()}
Admin.py:
class CustomCategoryAdmin(admin.ModelAdmin):
form = CategoryForm
def get_form(self, request, obj=None, **kwargs):
form = super().get_form(request, obj, **kwargs)
if not request.user.is_superuser:
self.fields = ('category',)
else:
self.fields = ('category', 'company',) # this throws a key error because company is excluded
return form
def save_related(self, request, form, formsets, change):
super(CustomCategoryAdmin, self).save_related(request, form, formsets, change)
company = request.user.company
form.instance.company.add(company) # add object to company field while saving
What I decided to do was create another form named AdminCategoryForm and set the form = AdminCategoryForm as an attribute of CustomCategoryAdmin.
So now my Forms.py:
class CategoryForm(forms.ModelForm):
class Meta:
model = Category
fields = ('category', 'company',)
exclude = ['company']
widgets = {
'company': forms.HiddenInput()
}
class AdminCategoryForm(forms.ModelForm):
class Meta:
model = Category
fields = ('category', 'company',)
And my Admin.py:
class CustomCategoryAdmin(admin.ModelAdmin):
form = AdminCategoryForm
def get_form(self, request, obj=None, **kwargs):
form = super().get_form(request, obj, **kwargs)
if not request.user.is_superuser:
self.fields = ('category',)
else:
self.fields = ('category', 'company',)
self.filter_horizontal = ('company',)
return form
def save_related(self, request, form, formsets, change):
super(CustomCategoryAdmin, self).save_related(request, form, formsets, change)
company = request.user.company
form.instance.company.add(company)
Even though I am repeating my code I feel that this may be the more elegant solution than overriding a ModelAdmin method as it becomes much more clear to outside programmers what is going on here.
If anybody else has other solutions I would love to hear..
I want to set the item in dropdown using the query in the form. I want to add employee and the select company which using filter Is_Del= 0. I do not know how to set values for the drop down and where to write this query.
I tried to put in Forms.py, but it is not working.
This is form.py
class EmployeeCreateForm(forms.ModelForm):
class Meta:
model = Employee
fields = ('Emp_Name','Emp_company','Emp_Dept','Emp_Join_Date', 'Emp_End_Date')
def clean(self):
cleaned_data = super(EmployeeCreateForm, self).clean()
Emp_Name = cleaned_data.get('Emp_Name')
Emp_company = cleaned_data.get('Emp_company')
Emp_Dept = cleaned_data.get('Emp_Dept')
Emp_Join_Date = cleaned_data.get('Emp_Join_Date')
Emp_End_Date = cleaned_data.get('Emp_End_Date')
return cleaned_data
def __init__(self, *args, **kwargs):
super(EmployeeCreateForm,self).__init__(*args, **kwargs)
self.fields['Emp_company'].queryset = Company.objects.filter(Is_Del=0)
and below is my view.py
class EmployeeCraeteView(LoginRequiredMixin,SuccessMessageMixin,CreateView):
model=Employee
form = EmployeeCreateForm
success_message = " Employee Craeted successfully!"
success_url="../../company/all-companies"
template_name = 'employee_form.html'
fields =[
'Emp_Name','Emp_company','Emp_Dept','Emp_Join_Date',
'Emp_End_Date'
]
companies= Company.objects.filter(Is_Del=0)
def form_valid(self,form):
form.instance.Emp_Crt_By = self.request.user
if form.cleaned_data['Emp_Join_Date'] >= form.cleaned_data['Emp_End_Date']:
form.add_error('Emp_End_Date', 'Joining date should be less than Ending date')
return self.form_invalid(form)
return super(EmployeeCraeteView, self).form_valid(form)
I want to show only this companies in the form which are filtered by Is_Del =0
Your EmployeeCreateView is wrong:
Remove the attributes form, fields and companies
Add form_class = EmployeeCreateForm.
The reason is that form doesn't do anything in a CreateView (see here). To use a custom form class, you need to pass it to form_class.
Your CreateView was dynamically creating the form using a modelform_factory with the fields you defined (if you hadn't added those you'd have seen your mistake immediately) and so your EmployeeCreateForm is never instantiated.
I do not want the logged in user to show up on this ModelMultipleChoiceField in order to restrict themselves from creating a following relationship with themselves? So how do I exclude the logged in user from the queryset, probably an easy fix but I'm new to Django and it has eluded me for a few hours now.
forms.py
class Add_Profile(forms.ModelForm):
def __init__(self,*args, **kwargs): # initializing your form in other words loading it
super(Add_Profile, self).__init__(*args, **kwargs)
user_id = kwargs.pop('user_id') # taking user_id out of the querylist
self.fields['follows'] = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple(), queryset=UserProfile.objects.filter(~Q(id=user_id)))
class Meta:
model = UserProfile
fields = (
'bio',
'follows',
'theme',
'profile_picture',
)
Views.py
#login_required
def edit_profile(request, user_id):
userprofile = UserProfile.objects.get(pk=user_id)
if request.method == 'POST':
edit_profile = Add_Profile(request.POST, request.FILES, instance=userprofile, user_id=request.user.id)
if edit_profile.is_valid():
edit_profile.save()
return redirect('/home/user/{0}/'.format(request.user.username))
else:
print edit_profile.errors
else:
edit_profile = Add_Profile(instance=userprofile, user_id=request.user.id)
return render (request, 'edit.html', {'form': edit_profile,})
Error: init() got an unexpected keyword argument 'user_id'
You can definitely do it using forms.Form instead of forms.ModelForm with something along the lines of this example in the docs:
from django import forms
from django.contrib.auth import get_user_model
class Add_Profile(forms.Form):
follows = forms.ModelMultipleChoiceField(queryset=None)
def __init__(self, user=None, *args, **kwargs):
super(Add_Profile, self).__init__(*args, **kwargs)
if user is not None:
self.fields['follows'].queryset = get_user_model().objects.exclude(pk=user.pk)
else:
self.fields['follows'].queryset = get_user_model.objects.all()
Just pass in the user you wish to exclude when you instantiate the form:
form = Add_Profile() # all users will be present in the dropdown
some_guy = User.objects.get(pk=4)
form = Add_Profile(user=some_guy) # all users except some_guy will be present
Define an __init__ method for the form class. Pass the logged in userid to the form while initializing it, this will work with a model form.
def __init__(self, *args, **kwargs):
user_id = kwargs.pop('user_id')
super(Add_Profile, self).__init__(*args, **kwargs)
self.fields['follows'] = forms.ModelMultipleChoiceField(queryset=UserProfile.objects.filter(~Q(user_id=user_id)))
While initializing your form, you can pass user_id
address_form = Add_Profile(request.POST, user_id=request.user.id)