Getting an ID from a one-to-many field - django

I am trying to get a ID for a related Model. I have tried several things but can not figure it out. below is the code I am using.
def assign_load(request):
form = DispatchForm(request.POST or None)
loads = Load.objects.all().filter(active=True, dispatched=False, picked_up=False, delivered=False,
billed=False, paid=False)
context_dict = {'dispatch' : form, 'load' : loads}
if form.is_valid():
save_it = form.save()
save_it.save()
new_dispatch = Dispatch.objects.all().filter(id=save_it.id)
print(new_dispatch.model.load_number.pk)
return HttpResponseRedirect('/dispatch/dispatch/')
return render(request, 'dispatch/dispatch_form.html', context_dict)
new_dispatch is the newly created record that has a one-to-many releation with a model called Loads. I need to get the load_number PK when I try to print just the new_dispatch.model.load_number I get back the following.
<django.db.models.fields.related_descriptors.ForwardManyToOneDescriptor object at 0x0000029016E34710>
but what I need is the ID for that record so that I can updated some fields.
Thank you very much for your help, I am new to the Django world and trying to figure this stuff out.

Use Dispatch.objects.get(id=save_it.id) instead of Dispatch.objects.all().filter(id=save_it.id)

Related

(Hidden field id) Select a valid choice. That choice is not one of the available choices. (Django)

I'm receiving this error when I try to submit two formsets. After I fill the form and click the save button, it gives the error:
(Hidden field id) Select a valid choice. That choice is not one of the available choices.
I'm trying to create dynamic form so that the user can add new sections and also new lectures inside the section when they click "Add" button. The adding new form function works well, I just have problem saving it to the database.
Views.py
def addMaterials(request, pk):
course = Course.objects.get(id=pk)
sections = CourseSection.objects.filter(course_id=pk)
materials = CourseMaterial.objects.filter(section__in=sections)
SectionFormSet = modelformset_factory(CourseSection, form=SectionForm, extra=0)
sectionformset = SectionFormSet(request.POST or None, queryset=sections)
MaterialFormSet = modelformset_factory(CourseMaterial, form=MaterialForm, extra=0)
materialformset = MaterialFormSet(request.POST or None, queryset=materials)
context = {
'course': course,
'sectionformset': sectionformset,
'materialformset': materialformset,
}
if request.method == "POST":
if all([sectionformset.is_valid() and materialformset.is_valid()]):
for sectionform in sectionformset:
section = sectionform.save(commit=False)
section.course_id = course.id
section.save()
for materialform in materialformset:
material = materialform.save(commit=False)
print(material)
material.section_id = section #section.id or section.pk also doesn't work
material.save()
return('success')
return render(request, 'courses/add_materials.html', context)
Forms.py
class SectionForm(forms.ModelForm):
class Meta:
model = CourseSection
fields = ['section_name', ]
exclude = ('course_id', )
class MaterialForm(forms.ModelForm):
class Meta:
model = CourseMaterial
fields = ['lecture_name', 'contents']
The second formset which is materialformset need the section id from the first formset hence why there is two loop in views.
Can someone help me to solve this. I'm not sure how to fix it.
This is the what I'm trying to do.
I'm new to django but I had to face with the same problem. My solution was to handle singularly each formset inside 'views.py'.
In the template.html, create a tag for each formset you have, than inside that tag put <input type="submit" name="form1">(Note that name is important and must be different with the respect of the form you are submitting).
Then in views.py, instead for writing if all([sectionformset.is_valid() and materialformset.is_valid()]), try like this:
if 'form1' in request.POST:
if sectionformset.is_valid():
sectionformset.save()
# other rows of your code
return('success')
if 'form2' in request.POST:
if materialformset.is_valid():
materialformset.save()
# etc. etc.

Django inline-formset ordering issue when editing

I am trying to use the Django inline-formset.
Forms should be displayed sorted by their order value which is correctly done when I request the form.
But if I change the order values and save, the first view is with the previous order (refresh does the trick)
forms:
class SlidesForm(forms.ModelForm):
order = forms.IntegerField(widget=forms.NumberInput())
background_image = forms.ImageField(widget=forms.FileInput(attrs={'class': 'custom-file-input'}), required=False)
text = forms.CharField(max_length=256, widget=forms.Textarea(attrs={'rows': 2, 'class': 'form-control'}), required=False)
class Meta:
model = SlideCarousel
fields = ['order', 'background_image', 'text']
views:
def management_form_general(request, city_slug):
city = City.objects.get(slug=city_slug)
SlideCarouselInlineFormSet = inlineformset_factory(City, SlideCarousel, form=SlidesForm, extra=0)
if request.method == 'POST':
carousel_formset = SlideCarouselInlineFormSet(request.POST, request.FILES, instance=city, queryset=city.slidecarousel_set.order_by("order"))
if carousel_formset.is_valid():
carousel_formset.save()
else:
carousel_formset = SlideCarouselInlineFormSet(instance=city, queryset=city.slidecarousel_set.order_by("order"))
return render(request, 'management/form/city_general.html', {'city': city, 'carousel_formset': carousel_formset})
Any idea what I am doing wrong ? Tried to reinstance the carousel_formset after the save but it seems nasty and it actually didn't work
Right now you're still returning the same queryset (already evaluated and ordered) in the formset. What you need is to get the data that you just saved and update the formset with it. I think that you have two options that should work.
Recreate the carousel_formset like you said. This might not be exactly what you want but it seems more likely than my second suggestion. You said you tried this and it didn't work. If your code looks the same as mine then you might want to skip this approach.
carousel_formset.save()
carousel_formset = SlideCarouselInlineFormSet(
instance=city,
queryset=city.slidecarousel_set.order_by("order"),
)
Usually, after I save a form(set) I would redirect to a success URL. In this case that would be the same path again.
carousel_formset.save()
return redirect(request.path)
A third option that I have no idea if it will work, but you could try for very little effort, would be to re-set the carousel_formset.queryset attribute.
carousel_formset.save()
carousel_formset.queryset = city.slidecarousel_set.order_by("order")

Generate short temporary link to a form

So, I have a form that logged in user can fill, and when that form is submitted, I'd like it to generate a temporary link to another form that non-logged in users could access and fill, that would last 24 hours. That form would then send data to the database. That link would be valid only several times. that number is determined in the first form, by the logged in user.
I would like that URL to be as short as possible and easy to write down manually on a browser, but I don't know the best way to do so, or even how to do so.
The first form is completed, and only logged in user can fill it, but I do not know how to generate that temporary link.
Please keep in mind that I am fairly new to Django.
Views.py
class GenFormPageView(LoginRequiredMixin, TemplateView):
login_url = '/'
def get(selfself, request, **kwargs):
if request.method == 'POST':
formation_form = genForm(data=request.POST)
if formation_form.is_valid():
cdata = formation_form.cleaned_data.get
else:
print('Invalid')
else:
formation_form = genForm()
return render(request, 'genform.html', {'formation_form': formation_form})
Forms.py
class genForm(forms.ModelForm):
liste_formation = []
formations_info = {}
for formation in Formation.objects.all():
if formation.libelle in formations_info:
formations_info[formation.libelle].append(formation.formateur, formation.dateDebut, formation.dateFin,
formation.nbJours, formation.cours)
else:
info = {'formateur':formation.formateur.get_full_name(), 'dateDebut':formation.get_debut(), 'dateFin':formation.get_fin(), 'nbJours':formation.nbJours, 'cours':formation.cours.get_titre()}
formations_info[formation.libelle] = info
liste_formation.append(formations_info)
formations = [str(formation) for formation in Formation.objects.all()]
formation_select = forms.ChoiceField(label='Formation', choices=([(formation, formation) for formation in formations]), required = True)
formateur = forms.CharField(label='Formateur')
cours = forms.CharField(label='Cours')
nombre_eleves = forms.IntegerField(label='Nombre d\'élèves', min_value=1)
formations = json.dumps(formations_info)
class Meta:
model = Formation
fields = ('formation_select', 'formateur', 'dateDebut', 'dateFin', 'nbJours', 'cours', 'nombre_eleves')
urls.py
url(r'^genform/$', views.GenFormPageView.as_view()),
Here is the field tht is going to determine the number of time the new form can be submitted
When I click the button 'Generer', it creates a link to a form. that form can only be submitted a fixed number of time, and the link can only lead to that form if clicked within 24 hours from the generated time.
What I am trying right now, is creating a field in my database containing the time when the form is generated, and then I will check if the time is valid, and display the form only if it is.
That my idea right now, but I still don't konw how to do it.
So, to answer my question, I finally decided to use a field in my database containing the date of the creation of the form, then I would just check that today's date is within 24 hours from the date in my database.

Django inline formset: The inline foreign key did not match the parent instance primary key

There seem to be a lot of questions out there on this topic, but I haven't been able to find anything that has solved this problem for me. I am trying to make a view to create Recipe object, which has foreign key sets for Ingredients and Instructions. When I try to submit, the formsets give me the error 'recipe': [u'The inline foreign key did not match the parent instance primary key.']. Here's my complete view:
def recipe_add(request):
profile = profile_from_request(request)
if request.method == 'POST':
recipe_form = RecipeForm(request.POST)
if recipe_form.is_valid():
recipe = recipe_form.save(commit=False)
recipe.user = profile_from_request(request)
ingredient_form = IngredientFormSet(request.POST, prefix='ingredient', instance=recipe)
instruction_form = InstructionFormSet(request.POST, prefix='instruction', instance=recipe)
if ingredient_form.is_valid() and instruction_form.is_valid():
recipe = recipe.save()
ingredient_form.save()
instruction_form.save()
messages.success(request, _("Recipe added."))
return HttpResponseRedirect(reverse("recipes:recipe_list"))
else: # GET
recipe_form = RecipeForm()
ingredient_form = IngredientFormSet(prefix='ingredient', instance=Recipe())
instruction_form = InstructionFormSet(prefix='instruction', instance=Recipe())
return render(
request, 'recipes/recipe_add.html',
{
'profile': profile,
'recipe_form': recipe_form,
'ingredient_form': ingredient_form,
'instruction_form': instruction_form,
}
)
I'm not sure if the issue comes from creating the formsets in the GET or POST methods. I've tried messing with the instance argument in both but haven't gotten anything to work.
I had a similar issue because I forgot to add the management form for my formset in my template.
In your templates, you must have something like this :
{{ ingredient.management_form }}
{{ instruction.management_form }}
Quick way to verify is to inspect your form code in your browser and look for the hidden fields that this management form is rendering into.
You can then see if the foreign key value of those formsets matches the recipe primary key which may point you in the right direction.

submitting form results into db - django

i created a form to save a post into db for my blog project. I've designed index page. now i am tryin to create a form to create new posts. before that i was using ' manage.py shell'
here is my view :
def addpost(request):
form = addForm()
if request.method=="POST":
titleform = request.POST['title']
bodyform = request.POST['body']
checkform = request.POST['isdraft']
if form.is_valid():
n = Post(title = titleform, body = bodyform, isdraft=checkform)
n.save()
return HttpResponseRedirect('/admin/')
else:
pass
return render(request,'userside/add.html',{'form':form,})
my model.py:
class Post(models.Model):
title = models.CharField(max_length = 100)
body = models.TextField()
slug = AutoSlugField(populate_from='title',unique=True)
posted = models.DateField(auto_now_add=True)
isdraft = models.BooleanField()
def __unicode__(self):
return self.title
#permalink
def get_absolute_url(self):
return ('view_blog_post',None, {'postslug':self.slug})
class addForm(forms.Form):
title = forms.CharField(max_length=100)
body = forms.CharField(widget=forms.Textarea)
isdraft = forms.BooleanField()
if i submit form as 'isdraft' field is False(unchecked) ; it gives error like:
MultiValueDictKeyError at /admin/addpost/
"Key 'isdraft' not found in "
and if i submit the form as 'isdraft' field is True(checked) ; it gives nothing. just refreshing form. no adding data into db.
i am doing sth wrong..
thank you
edit : Dmitry Beransky's answer worked for checkbox error. but it still doesnt add any data into db. just refreshes the form.
The whole point of using a form is that it takes care of validation and cleaning, that is converting values to the proper data types. That's why you should be accessing form.cleaned_data rather than reques.POST, and you should be doing it inside the if form.is_valid() check.
Edit
I've just noticed that you're never passing request.POST to the form. So form.is_valid() will never be true.
Please go back and read the documentation about using a form in a view.
If a checkbox is not checked in your HTML form, it's name/value is not going to be included in the data that the browser sends to your server. Which meanst that the request.POST dictionary is not going to contain an entry for 'isdraft' which in turn will cause a key error when you try to read the isdraft value. A solution is to change the way you read the value from the posted data to:
checkform = request.POST.get('isdraft', False)
rather than throw an error if isdraft isn't found in the dictionary, this will set checkform to False (the default value in case of a missing key)
Maybe your form does not validate at all. Have you checked if your code even reaches those lines after the if form.is_valid() statement ? If they do, what you've done there is right and should create the db row for your new entry, though you could have used
Post.objects.create(....) , and that would have taken away the need for calling the method save().
Some points though:
instead of checking for request.POST , check for request.method == 'POST' , cause there might be a post which has an empty POST dict ( in case no arguments have been submitted ), in that case request.POST fails to provide the right check .
see the docs for more info : request.POST
instead of using request.POST['var_name'] , use request.POST.get('var_name', 'default_value') , cause doing this like request.POST['var_name'] might result in some exceptions ( in case for example the argument is not provided , like what happened for your checkform variable )
Try accessing those variables through form.cleaned_data
and finally , you don't need the else statement in the end , just use the indentation :)