I am trying to achieve a rather simple thing, but got stuck with an error and have no idea where does it come from.
I want to create and save an object in my views. The code is very simple:
models.py:
class Iteration(models.Model):
user = models.ForeignKey(User)
one_two = '1-2 weeks'
two_four = '2-4 weeks'
four_six = '4-6 weeks'
six_eight = '6-8 weeks'
DURATION_CHOICES = (
(one_two, '1-2 weeks'),
(two_four, '2-4 weeks'),
(four_six, '4-6 weeks'),
(six_eight, '6-8 weeks'),
)
duration = models.CharField(max_length=100, choices=DURATION_CHOICES, default=two_four)
project = models.ForeignKey(Project)
def is_upperclass(self):
return self.duration in (self.one_two, self.six_eight)
views.py:
def New_iteration(request, slug):
form = IterationForm()
user = request.user
project = Project.objects.get(user=user, slug=slug)
if request.method == 'POST':
form = IterationForm(request.POST)
errors = form.errors
if form.is_valid:
user = request.user
duration = request.POST['duration']
project = Project.objects.get(user=user, slug=slug)
new_iteration = Iteration(user.id, form.cleaned_data['duration'], project.id)
new_iteration.save()
return HttpResponseRedirect("/dashboard/")
else:
return HttpResponse("not valid")
return render(request, "new_iteration.html", {"form" : form, "project" : project, "user" : user})
I am receiving an error invalid literal for int() with base 10: '2-4 weeks'. I think it comes from
new_iteration = Iteration(user.id, form.cleaned_data['duration'], project.id)
line, but I'm not sure what to do.
You shouldn't create the object as
new_iteration = Iteration(user.id, form.cleaned_data['duration'], project.id)
you need to pass data as keyword parameters as
new_iteration = Iteration(user = user, duration = form.cleaned_data['duration'],
project = project)
However, I believe IterationForm is model form and you want to get project before saving the iteration, more better way is
if form.is_valid(): #note this is function call
user = request.user
project = Project.objects.get(user=user, slug=slug)
new_iteration = form.save(commit=False)
new_iteration.project = project
new_iteration.save()
I have solved the task. I should have added my forms.py for better understanding. I have edited my forms.py file and defined there, that the only "selectable" field should be "duration", and Django should get other stuff (user and project) when initiating the form in views.
The other mistake was that I did not pass data as keyword parameters, thanks Rohan.
So I have added fields = ('duration',) to my ModelForm and re-initiaded the form with keyword arguments now.
Related
I'm doing a edit form for some data. I'm having problem to pass information to . As you can see in my view, I pass the data to form using "initial" dictionary.
VIEWS.PY
#login_required
def project_detail(request, project_id):
if request.method == 'POST':
project = get_object_or_404(Project, pk=project_id)
form = ProjectDetailForm(project_id, request.POST, instance = project)
if form.is_valid():
instance = form.save(commit=False)
instance.client = Project.objects.get(pk=project_id).client
form.save()
messages.success(request,'Projeto modificado')
return redirect('projects')
else:
messages.error(request,'Ocorreu um erro!')
else:
project = get_object_or_404(Project, pk=project_id)
form = ProjectDetailForm(project_id, initial={'modal':project.modal,
'culture':project.culture,
'owner':project.owner,
'value':project.value,
'final_date':project.final_date,
'text':project.text,
'status':project.status,
'farm':project.farm.values()})
return render(request,'project_detail.html',{'form':form})
But doing this, the data is not displaied in . Thinking here, ManyToManyField saves data in lists. I tried iterate this field but still not working and I guess thats not the best way to do this.
MODELS.PY
class Project(models.Model):
modal_types = [('CUSTEIO AGRÍCOLA','Custeio Agrícola'),('CUSTEIO PECUÁRIO','Custeio Pecuário'),('INVESTIMENTO AGRÍCOLA','Investimento Agrícola'),('INVESTIMENTO PECUÁRIO','Investimento Pecuário'),('FGPP','FGPP')]
status_opts = [('Análise','Análise'),('Desenvolvimento','Desenvolvimento'),('Processamento','Processamento'),('Liberação','Liberação'),('Finalizado','Finalizado'),('Cancelado','Cancelado'),('Suspenso','Suspenso')]
farm = models.ManyToManyField(Farm, related_name='farm_name',verbose_name='Propriedade beneficiada')
client = models.ForeignKey(Clients, on_delete=models.CASCADE, related_name='project_client',default=None,null=True, verbose_name='Cliente')
owner = models.ForeignKey(Owner, on_delete=models.CASCADE, related_name='project_bidder',default=None,null=True, verbose_name='Proponente')
warranty = models.ManyToManyField(Farm, related_name='project_warranty',default=None, verbose_name='Propriedade de garantia')
modal = models.CharField(max_length=100,default=None,choices=modal_types, null=True, verbose_name='Tipo')
culture = models.CharField(max_length=50,null=True, verbose_name='Cultura')
status = models.CharField(max_length=50,null=True, verbose_name='Status', choices=status_opts)
created_date = models.DateField(null=True, verbose_name='Data de criação')
value = models.FloatField(max_length=10,null=True, verbose_name='Valor financiado')
final_date = models.DateField(default=None,null=True, verbose_name='Fim do contrato')
text = models.TextField(default=None,null=True, verbose_name='Observações')
forms.py
class ProjectDetailForm(ModelForm):
class Meta:
model = Project
fields = ['status','owner', 'farm', 'warranty', 'modal', 'culture', 'value','final_date','text']
def __init__(self, project_id, *args, **kwargs):
client_id = Project.objects.get(pk=project_id).client
super(ProjectDetailForm,self).__init__(*args,**kwargs)
self.fields['value'].required = False
self.fields['final_date'].required = False
self.fields['text'].required = False
self.fields['farm'].queryset = Farm.objects.filter(client=client_id)
self.fields['warranty'].queryset = Farm.objects.filter(client=client_id)
for field_name, field in self.fields.items():
field.widget.attrs['class'] = 'form-control'
Here all the fields with information, but in "select" nothing is selected, despite having data in the database
Someone can help me?
Why are you passing 'project_id' into your form class instance? Try changing this:
form = ProjectDetailForm(project_id, request.POST, instance = project)
to this:
form = ProjectDetailForm(request.POST, instance = project)
and see if it helps. Also in your form initialization, I'm not sure you're using the "initial=" values dictionary correctly. Initial values are typically defaults applicable to the "create" view, not database records that it sounds like you want to see in an update view. I think you want to pass in the instance of your database record there, something like:
else:
project = get_object_or_404(Project, pk=project_id)
form = ProjectDetailForm(instance=project)
Also, you really don't need to write project object query twice in this view. You can do something like:
def project_detail(request, project_id):
project = get_object_or_404(Project, pk=project_id) # query this once here
if request.method == 'POST':
form = ProjectDetailForm(request.POST, instance=project)
if form.is_valid():
instance = form.save(commit=False)
instance.client = Project.objects.get(pk=project_id).client
form.save()
messages.success(request,'Projeto modificado')
return redirect('projects')
else:
messages.error(request,'Ocorreu um erro!')
# you probably want a redirect here as well
else:
form = ProjectDetailForm(instance=project)
return render(request,'project_detail.html',{'form':form})
Finally, if you're trying to limit the choices from your ManyToMany field in the user's form, you can do so with something like this:
class ProjectDetailForm(forms.ModelForm):
class Meta:
model = YourModelName
fields = ['farm']
farm = forms.ModelMultipleChoiceField(
queryset=Farm.objects.filter(some_field=some_criteria).order_by('some_field'),
widget=forms.CheckboxSelectMultiple)
More info about widgets on form classes here in Django docs.
I am building a BlogApp and I am stuck on an Error and I keep getting this error :-
The Post could not be created because the data didn't validate.
What i am trying to do
I made a feature that users cannot insert past date in DateTimeField. AND If user enters the past date in field then a Validation Error should be display. AND i was testing it and suddenly a new error is occured.
forms.py
class PostForm(forms.ModelForm):
date = forms.DateTimeField(initial=timezone.now)
def clean_date(self):
date = self.cleaned_data['date']
if date.date() < datetime.date.today():
raise forms.ValidationError("The date cannot be in the past!")
return date
views.py
def new__blog_post(request,user_id):
if request.method != 'POST':
form = PostForm()
else:
form = PostForm(request.POST,request.FILES)
new_post = form.save()
new_post.post_owner = request.user
new_post.save()
return redirect('mains:posts',user_id=user_id)
context = {'form':form,'posts':posts}
return render(request, 'new_blog_post.html', context)
models.py
class BlogPost(models.Model):
post_owner = models.ForeignKey(User,default='',null=True,on_delete = models.CASCADE)
post_title = models.CharField(max_length=500,default='')
date_added = models.DateTimeField(null=True,default=timezone.now)
It is showing that the Error is in new_post = form.save() line . It means that there's having problem in saving form.
The Problem
When i set the time of now ( as timezone.now as default ) in DateTimeField then it is working fine , Post is saving fine. BUT when i change the present time (2021-02-18) into past time (2019-02-18) then it is showing that error The Post could not be created because the data didn't validate .
I don't know what am i doing wrong
Any help would be appreciated.
Thank You in Advance
To validate data you need to call Form.is_valid(). See Using forms to validate data.
So call first is_valid():
def new__blog_post(request,user_id):
if request.method == 'POST':
form = PostForm(request.POST,request.FILES)
if form.is_valid():
new_post = form.save()
new_post.post_owner = request.user
new_post.save()
return redirect('mains:posts',user_id=user_id)
else:
form = PostForm()
context = {'form':form}
return render(request, 'new_blog_post.html', context)
The name of clean method must be changed - forms.py:
from django.core.exceptions import ValidationError
import datetime
class PostForm(forms.ModelForm):
class Meta:
model = BlogPost
fields = ["post_title","date_added"]
def clean_date_added(self):
date_added = self.cleaned_data['date_added']
if date_added.date() < datetime.added.today():
raise forms.ValidationError("The date cannot be in the past!")
return date_added
It sound simple but I want my page to display the database of my model.
I use ModelForm for user to input and it would save into my model. Now I want to render the whole table, not just each separately.
forms.py
class My_Folio(forms.ModelForm):
class Meta:
model = my_data
fields = ['symbol','buy_price','quantity']
views.py
def blockfolio(request):
if request.method == 'POST':
my_folio = My_Folio(request.POST)
if my_folio.is_valid():
symbol = my_folio.cleaned_data['symbol']
buy_price = my_folio.cleaned_data['buy_price']
quantity = my_folio.cleaned_data['quantity']
instance = my_folio.save(commit=False)
instance.save()
return render(request, 'blockfolio/blockfolio.html', {'symbol':symbol, 'buy_price':buy_price, 'quantity':quantity, 'instance':instance})
template:
{{instance}} This give me the user input after submit, but I want to show all of the inputs saved in database.
Maybe your post is invalid. Try this code in views.py.
def blockfolio(request):
if request.method == 'POST':
my_folio = My_Folio(request.POST)
if my_folio.is_valid():
symbol = my_folio.cleaned_data['symbol']
buy_price = my_folio.cleaned_data['buy_price']
quantity = my_folio.cleaned_data['quantity']
instance = my_folio.save(commit=False)
instance.save()
return render(request, 'blockfolio/blockfolio.html', {'symbol':symbol, 'buy_price':buy_price, 'quantity':quantity, 'instance':instance})
else:
print("request is invalid: {}".format(request.POST))
return "bad request!", 500
I'm building a test aplication to insert data into database and I keep on getting error "local variable 'form' referenced before assignment". I read a few articles and I do not understand how should I approach the problem in case the method is not POST.
All I'm trying to acchieve with this test application is to go to specified page, insert some numbers in the fields and then submit them. After that I want to check DB if they got submitted correctly.
views.py
def cost(request):
if request.method == 'POST':
form = CostForm(request.POST)
if form.is_valid():
amount = request.POST.get('amount', '')
cost = request.POST.get('cost', '')
cost_obj = Cost(amount = amount, cost = cost)
cost_obj.save()
return HttpResponseRedirect('/vnos/')
else:
form = CostForm()
return render(request, "cost.html", {'form': form})
models.py
class Cost(models.Model):
cost = models.FloatField()
amount = models.FloatField()
forms.py
class CostForm(forms.Form):
date = forms.DateField()
amount = forms.FloatField()
class Meta:
model = Cost
fields = ['cost', 'amount']
Your else statement is indented too far. Move it back one level.
Two other points: you should be getting the data from the form after validation, not direct from the POST:
amount = form.cleaned_data['amount']
cost = form.cleaned_data['cost']
And secondly, you could make it even simpler by using a ModelForm.
I have a Client and Groupe Model.
A Client can be part of multiple groups.
Clients that are part of a group can use its group's free rental rate at anytime but only once. That is where the intermediary model (ClientGroupe) comes in with that extra data.
For now, when I try to save the m2m data, it just dies and says I should use the ClientGroupe Manager...so what's missing?
Here are my models:
class Groupe(models.Model):
nom = models.CharField(max_length=1500, blank=True)
class Client(models.Model):
nom = models.CharField(max_length=450, blank=True)
prenom = models.CharField(max_length=450, blank=True)
groupes = models.ManyToManyField(Groupe, null = True, blank = True, through='ClientGroupe')
class ClientGroupe(models.Model):
client = models.ForeignKey(Client)
groupe = models.ForeignKey(Groupe)
dt = models.DateField(null=True, blank=True) # the date the client is using its group's free rental rate
class Meta:
db_table = u'clients_groupes'
and here's my view:
def modifier(request, id):
client = Client.objects.get(id=id)
form = ClientForm(instance = client)
dict = {
"form": form
, "instance" : client
}
if request.method == "POST":
form = ClientForm(request.POST, instance = client)
if form.is_valid():
client_mod = form.save()
id = client_mod.id
return HttpResponseRedirect(
"/client/%(id)s/?err=success" % {"id" : id}
)
else:
return HttpResponseRedirect(
"/client/%(id)s/?err=warning" % {"id" : id}
)
return render_to_response(
"client/modifier.html"
, dict
, context_instance=RequestContext(request)
)
EDIT:
and here's the ClientForm code:
class ClientForm(ModelForm):
class Meta:
model = Client
EDIT #2:
here's the error message:
AttributeError at /client/445/
Cannot set values on a ManyToManyField which specifies an intermediary model. Use ClientGroupe's Manager instead.
Request Method: POST
Request URL: http://localhost/client/445/
Exception Type: AttributeError
Exception Value: Cannot set values on a ManyToManyField which specifies an intermediary model. Use ClientGroupe's Manager instead.
Exception Location: C:\Python25\lib\site-packages\django\db\models\fields\related.py in __set__, line 574
Python Executable: C:\xampp\apache\bin\apache.exe
Python Version: 2.5.2
If you use the save method right now, Django will try to save using the manager (which Django doesn't allow). Unfortunately, the behavior you want is a little bit trickier than what ModelForm does by default. What you need to do is create a formset.
First of all, you will need to change the options of your ClientForm so that it doesn't display the groupes attribute.
class ClientForm(ModelForm):
class Meta:
model = Client
exclude = ('groupes',)
Next, you must change the view to display the formset:
from django.forms.models import inlineformset_factory
def modifier(request, id):
client = Client.objects.get(id=id)
form = ClientForm(instance = client)
# Create the formset class
GroupeFormset = inlineformset_factory(Client, Groupe)
# Create the formset
formset = GroupeFormset(instance = client)
dict = {
"form": form
, "formset" : formset
, "instance" : client
}
if request.method == "POST":
form = ClientForm(request.POST, instance = client)
formset = GroupeFormset(request.POST, instance = client)
if form.is_valid() and formset.is_valid():
client_mod = form.save()
formset.save()
id = client_mod.id
return HttpResponseRedirect(
"/client/%(id)s/?err=success" % {"id" : id}
)
else:
return HttpResponseRedirect(
"/client/%(id)s/?err=warning" % {"id" : id}
)
return render_to_response(
"client/modifier.html"
, dict
, context_instance=RequestContext(request)
)
And obviously, you must also tweak your template to render the formset.
If you need any other advice on formsets, see these articles:
Model formsets
Formsets
…
if form.is_valid():
client_mod = form.save(commit=False)
client_mod.save()
for groupe in form.cleaned_data.get('groupes'):
clientgroupe = ClientGroupe(client=client_mod, groupe=groupe)
clientgroupe.save()
…
You probably need to remove the ManyToMany field from your Client model, or else carefully exclude it from your form. Unfortunately, the default widget for the ManyToMany field cannot populate the ClientGroupe Model properly (even if the missing field, dt, had been set to autonow=True). This is something you'll either need to break out into another form, or handle in your view.
When you save your form, you save Client object. Now if you want to assign client to the group you should do this:
clientgroupe = ClientGroupe.objects.create(client=client_instance, groupe=groupe_instance, dt=datetime.datetime.now())
where client_instance and groupe_instance your client and groupe objets.
I'm providing an alternative solution due to issues I encountered with forms_valid not being called:
class SplingCreate(forms.ModelForm):
class Meta:
model = SplingModel
fields = ('Link', 'Genres', 'Image', 'ImageURL',)
def save(self, commit=True):
from django.forms.models import save_instance
if self.instance.pk is None:
fail_message = 'created'
else:
fail_message = 'changed'
fields = set(self._meta.fields) - set(('Genres',))
instance = save_instance(self, self.instance, fields,
fail_message, commit, construct=False)
genres = self.cleaned_data.get('Genres')
for genre in genres:
SplingGenreModel.objects.get_or_create(spling=instance, genre=genre)
return instance
I've copied the logic from djangos forms/models.py, my field Genres is a manytomany with an intermediary table - I exclude it from the save_instance and then save it separately.