Django - redefine Form Select using pk URL - django

I need to set input (select) value using pk sended by URL.
Using __init__ in form, almost get the answer, but __init__ method is executed twice and clean my value.
Form:
class CrearDelitoForm(forms.ModelForm):
class Meta:
model = Delito
exclude = ()
def __init__(self, numero_pk = None, *args, **kwargs):
super(CrearDelitoForm, self).__init__(*args, **kwargs)
self.fields["imputado"].queryset = Imputado.objects.filter(numero_id = numero_pk)
DelitoFormset = inlineformset_factory(
Expediente,
Delito,
form=CrearDelitoForm,
extra=1,
can_delete=True,
fields=('imputado', 'delito', 'categoria'),
}
)
Views:
class CrearDelito(CreateView):
model = Delito
form_class = CrearDelitoForm
template_name = 'crear_delito.html'
def get_context_data(self,**kwargs):
context = super().get_context_data(**kwargs)
context['formset'] = DelitoFormset()
context['expedientes'] = Expediente.objects.filter(id = self.kwargs['pk'])
return context
def get_form_kwargs(self, **kwargs):
kwargs['numero_pk'] = self.kwargs['pk']
return kwargs
**
System check identified no issues (0 silenced).
June 08, 2020 - 11:33:57
Django version 2.2.12, using settings 'red.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
, ]>
**
I think problem is on context = super().get_context_data(**kwargs) but don't know why.

A CreateView makes a form, the form you specified in the form_class. It will furthermore pass the request.POST and request.FILES to the form, you thus do not have to construct a formset yourself, but let the CreateView do that.
You need to pass the dictionary to the form_kwargs to the form_kwargs of the formset, so the get_form_kwargs needs to be altered to:
class CrearDelito(CreateView):
model = Delito
form_class = DelitoFormset
template_name = 'crear_delito.html'
def get_context_data(self,**kwargs):
context = super().get_context_data(**kwargs)
context['expedientes'] = Expediente.objects.filter(id=self.kwargs['pk'])
context['formset'] = context['form']
return context
def get_form_kwargs(self, **kwargs):
form_kwargs = super().get_form_kwargs(**kwargs)
form_kwargs['form_kwargs'] = {'numero_pk': self.kwargs['pk']}
form_kwargs['instance'] = Expediente.objects.get(id=self.kwargs['pk'])
return form_kwargs
def get_success_url(self):
return reverse('repositorio:crear_victima', args=[request.POST.get('numero_id')])

Related

ModelForm object has no attribute 'fields'

I am using a django (3.0) ModelMultipleChoice field for a form. I am trying to modify the queryset to make some restrictions on it.
here is the views :
def nouvelle_tache(request,id_livrable):
livrable=Livrable.objects.get(pk=id_livrable)
projet = livrable.projet
if request.method == "POST":
form = NouvelleTache(request.POST,projet=projet)
tache = form.save(commit=False)
tache.livrable = livrable
tache.id_tache = livrable.id_derniere_tache() + Decimal(0.01)
tache.save()
form.save_m2m()
etat = Temps_etat_tache(etat=form.cleaned_data['etat_initial'],tache=tache)
etat.save()
return redirect('tache',tache.pk)
else:
form = NouvelleTache(projet=projet)
return render(request, 'application_gestion_projets_AMVALOR/nouvelle_tache.html', locals())
And the forms :
class NouvelleTache(forms.ModelForm):
def __init__(self, *args, **kwargs):
projet = kwargs.pop('projet', None)
queryset = Utilisateur.objects.all()
for utilisateur in projet.utilisateurs:
queryset = queryset.exclude(pk=utilisateur.pk)
self.fields['ressources'].queryset = queryset
super(NouvelleTache, self).__init__(*args, **kwargs)
ressources= forms.ModelMultipleChoiceField(queryset=Utilisateur.objects.all() ,widget =forms.CheckboxSelectMultiple )
etat_initial = forms.ModelChoiceField(queryset=Etat_tache.objects.none())
class Meta:
model = Tache
fields = ['libelle']
I have the followig error : 'NouvelleTache' object has no attribute 'fields'
I don't understand why because many other users seems to have similar code and it works.
Any help would be appreciate.
super(NouvelleTache, self).__init__(*args, **kwargs)
needs to be executed first, as the fields are set in the super class:
def __init__(self, *args, **kwargs):
projet = kwargs.pop('projet', None)
queryset = Utilisateur.objects.all()
for utilisateur in projet.utilisateurs:
queryset = queryset.exclude(pk=utilisateur.pk)
super(NouvelleTache, self).__init__(*args, **kwargs)
self.fields['ressources'].queryset = queryset

I'm trying to incorporate multiple models into a single django view; how can I access the context **kwargs

I've tried this:
class MyClass(LoginRequiredMixin, UpdateView, ListView):
model = models.my_model
fields = ['first_model_field', 'second_model_field']
template_name = 'app/template_name.html'
extra_context = {'second_model': models.second_model.objects.get(pk=self.kwargs['pk']),#didn't work
'third_model':models.third_model.objects.get(pk='pk'),#didn't work
'fourth_model':models.fourth_model.objects.get(foreign_key_id = 'unique_kwarg')}#didn't work.
I also have url's that contain both the /<int:pk>/ kwarg and the /<int:unique_kwarg>/ kwarg.
I am having trouble figuring out how to reference the url **kwarg object.
def form_valid(self, form):
form_template_id = self.kwargs.get(self.pk_url_kwarg)
form.instance.model_id = model.objects.get(pk=form_template_id)
return super().form_valid(form)
and
def get_context_data(self, **kwargs):
"""Insert the form into the context dict."""
if 'unique_kwarg' not in kwargs:
kwargs['unique_kwarg'] = (self.kwargs.get(self.unique_kwarg))
kwargs['model_id'] = (model.objects.get(id=self.kwargs.get(self.unique_kwarg)))
return super().get_context_data(**kwargs)

Django custom Form and GenericView

The following code is working but I wonder if there is a more elegant way of doing. I have to pass the specie_id so I can filter the breeds to the corresponding specie. I can pass the specie_id to the view but I also have the information in the Resident model ("specie").
both get() and post() have nearly the same code, passing the specie_id.
The View
class ResidentUpdate(UpdateView):
model = Resident
template_name = "administration/resident_form.html"
form_class = ResidentCreateForm
def get(self, request, pk):
initial = self.model.objects.get(id=pk)
form = self.form_class(instance=initial, specie_id=initial.specie.id)
return render(request, self.template_name, {"form": form})
def post(self, request, pk):
initial = self.model.objects.get(id=pk)
form = self.form_class(request.POST, specie_id=initial.specie.id, instance=initial)
if form.is_valid():
form.save()
return redirect("resident_detail", pk)
return render(request, self.template_name, {"form", form})
The Form
class ResidentCreateForm(ModelForm):
class Meta:
model = Resident
fields = [
"name",
"specie",
"breed",
"gender",
"gender_status",
"birth_date",
"organization",
"social_behaviors",
"notes",
]
widgets = {
"birth_date": DateInput(attrs={"class": "flatpickr"}),
"specie": HiddenInput(),
}
def __init__(self, *args, **kwargs):
self.specie_id = kwargs.pop("specie_id", None)
super(ResidentCreateForm, self).__init__(*args, **kwargs)
self.fields["specie"].initial = self.specie_id
self.fields["breed"].queryset = Breed.objects.for_specie(self.specie_id)
EDIT :
#Alasdair's answer is good and I think I perfected it a little more. My form is used for the create view too. So I added a check to see if I have the specie_id in kwargs (create) or if I have to use the specie_id from the instance (update)
def __init__(self, *args, **kwargs):
self.specie_id = kwargs.pop("specie_id", None)
super(ResidentForm, self).__init__(*args, **kwargs)
if not self.specie_id:
self.specie_id = self.instance.specie.id
self.fields["specie"].initial = self.specie_id
self.fields["breed"].queryset = Breed.objects.for_specie(self.specie_id)
It looks like you can do self.fields["breed"].queryset = Breed.objects.for_specie(self.initial.specie_id), then you don't need to pass in specie_id to the form.
class ResidentCreateForm(ModelForm):
class Meta:
model = Resident
fields = [
"name",
"specie",
"breed",
"gender",
"gender_status",
"birth_date",
"organization",
"social_behaviors",
"notes",
]
widgets = {
"birth_date": DateInput(attrs={"class": "flatpickr"}),
}
def __init__(self, *args, **kwargs):
super(ResidentCreateForm, self).__init__(*args, **kwargs)
self.fields["breed"].queryset = Breed.objects.for_specie(self.instance.specie_id)
Note I've removed the specie hidden input above, I don't think it's necessary.
The UpdateView takes care of passing instance to the form, so you can simplify the view.
from django.urls import reverse
class ResidentUpdate(UpdateView):
model = Resident
template_name = "administration/resident_form.html"
form_class = ResidentCreateForm
def get_success_url(self):
"""Redirect to resident_detail view after a successful update"""
return reverse('resident_detail', args=[self.kwargs['pk']]
I think a better approach would be overriding the get_form_kwargs method in your views.
def get_form_kwargs(self):
form_kwargs = super().get_form_kwargs()
form_kwargs.update({'specie_id': self.kwargs.get('specie_id')})
return form_kwargs

How to perform queries in django modelform?

I tried this in my modelform:
class Ledgerform(forms.ModelForm):
class Meta:
model = ledger1
fields = ('name', 'group1_Name')
def __init__(self, User, Company, *args, **kwargs):
self.User = kwargs.pop('User', None)
self.Company = kwargs.pop('Company', None)
super(Ledgerform, self).__init__(*args, **kwargs)
self.fields['name'].widget.attrs = {'class': 'form-control',}
self.fields['group1_Name'].queryset = group1.objects.filter(User= self.User,Company = self.Company)
In my views.py I have done something like this:
class ledger1ListView(LoginRequiredMixin,ListView):
model = ledger1
paginate_by = 15
def get_queryset(self):
return ledger1.objects.filter(User=self.request.user, Company=self.kwargs['pk'])
class ledger1CreateView(LoginRequiredMixin,CreateView):
form_class = Ledgerform
def form_valid(self, form):
form.instance.User = self.request.user
c = company.objects.get(pk=self.kwargs['pk'])
form.instance.Company = c
return super(ledger1CreateView, self).form_valid(form)
I want to perform the the same query that I have passed in my ledger1ListView by using queryset in my modelform but my kwargs.pop is not returning the current user or the company...
This is my models.py:
class ledger1(models.Model):
User = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,null=True,blank=True)
Company = models.ForeignKey(company,on_delete=models.CASCADE,null=True,blank=True,related_name='Companys')
name = models.CharField(max_length=32)
group1_Name = models.ForeignKey(group1,on_delete=models.CASCADE,blank=True,null=True)
Do any one know what I am doing wrong in my code?
Thank you in advance
You can override the FormMixin.get_form_kwargs [Django-doc] in your view, that it constructs a dictionary with the parameters necessary to initialize the form, like:
class ledger1CreateView(LoginRequiredMixin,CreateView):
form_class = Ledgerform
def get_form_kwargs(self):
data = super(ledger1CreateView, self).get_form_kwargs()
data.update(
User=self.request.User,
Company=company.objects.get(pk=self.kwargs['pk'])
)
return data
The form_valid function is called after the form is constructed, validated and appears to be valid. Typically it is used to redirect the user to the "success page".

ListView with Form in Django

I'm new to django framework developers, and I have read a lot of documentation of Class-Based View and Forms.
Now, I want to create a single page (for test purpose) that contains a list of cars and a Forms, at the bottom page, for create a new Car.
this is my views.py
class IndexView(ListView):
template_name = "index.html"
context_object_name = "cars"
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
context["form"] = CarForm
return context
def get_queryset(self):
self.brand = self.kwargs.pop("brand","")
if self.brand != "":
return Car.objects.filter(brand__iexact = self.brand)
else:
return Car.objects.all()
def post(self, request):
newCar = CarForm(request.POST)
if newCar.is_valid():
newCar.save()
return HttpResponseRedirect("")
else:
return render(request, "index.html", {"form": newCar})
class CarForm(ModelForm):
class Meta:
model = Car
delete = True
and this is a picture with what I want create.
image
My questions are:
1) this is a "Best-Pratice" for this purpose?
2) The {{ car.name.errors }} in my template are always blank (no validation error shows).
Thanks! … and sorry for my english.
You could go other way around. Create a FormView and put the list of cars in context. That way form handling becomes easier. Like this -
class CarForm(ModelForm):
class Meta:
model = Car
delete = True
class IndexView(FormView):
template_name = "index.html"
form_class = CarForm
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
# Pass the list of cars in context so that you can access it in template
context["cars"] = self.get_queryset()
return context
def get_queryset(self):
self.brand = self.kwargs.pop("brand","")
if self.brand != "":
return Car.objects.filter(brand__iexact = self.brand)
else:
return Car.objects.all()
def form_valid(self, form):
# Do what you'd do if form is valid
return super(IndexView, self).form_valid(form)