I'm trying to add functionality to add a IntegerField next to every 'stockItem' in the template so that the user can type how many of that item was needed and then update the 'count' value in the 'Stock' model. As for now it only works when the user only selects one item. (I know that how I implement this now would never work but I try to show how I intend it to work)
Models:
class Machine_Service(models.Model):
title = models.CharField(max_length = 100)
stockItem = models.ManyToManyField(Stock)
date = models.DateTimeField(default=timezone.now)
comment = models.TextField()
machine = models.ForeignKey(Machine, on_delete=models.CASCADE)
def __str__(self):
return self.title
class Stock(models.Model):
title = models.CharField(max_length=100)
count = models.IntegerField(default=10)
minLimit = models.IntegerField(default=1)
resellerCompany = models.CharField(max_length=100)
resellerPerson = models.CharField(max_length=100)
resellerEmail = models.EmailField()
def __str__(self):
return self.title
(I left the 'Machine' model out of this because it does not belong to the question)
view:
def CreateService(request):
if request.method == 'POST':
form = CreateServiceForm(request.POST)
if form.is_valid():
m = Machine.objects.get(id=form['machine'].value())
service = Machine_Service(title=form['title'].value(), date=form['date'].value(), comment=form['comment'].value(), machine=m)
service.save()
items = form['stockItem'].value()
for item in items:
s = Stock.objects.get(id=item)
service.stockItem.add(s)
service.save()
return redirect('Machines-Home')
else:
form = CreateServiceForm()
context = {
'form': form
}
form:
class CreateServiceForm(forms.ModelForm):
count = forms.IntegerField(required=False)
class Meta:
model = Machine_Service
fields = ['title', 'stockItem', 'count', 'date', 'comment', 'machine']
template:
(I do not need to use crispy)
{% extends "maskinNytt/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="content-section">
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">New Post!</legend>
{{ form|crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Post</button>
</div>
</form>
</div>
{% endblock content %}
In your case your models.py should look something like this:
class Machine_Service(models.Model):
stockItem = models.ManyToManyField(Stock, through='NeededItems')
...
class Stock(models.Model):
...
class NeededItem(models.Model):
machine_service = models.ForeignKey(Machine_Service, on_delete=models.CASCADE)
stock = models.ForeignKey(Stock, on_delete=models.CASCADE)
amount = models.IntegerField()
To specify this amount and use it in your views/forms you need to just create instances of NeededItem.
I assume you have a form for the MachineService and one for a NeededItem. For the NeededItems you need a FormSet, views.py:
from django import formset_factory
NeededItemFormset = formset_factory(NeededItemForm, extra=2)
def view(request):
if request.method == 'POST':
machine_form = MachineForm(request.POST, prefix='machine')
needed_item_formset = NeededItemFormset(request.POST, prefix='items')
if machine_form.is_valid() and needed_item_formset.is_valid():
machine_form.save()
needed_item_formset.save()
return redirect('blabla')
else:
machine_form = MachineForm()
needed_item_formset = NeededItemFormset()
return render('template', {'m_form': machine_form, 'n_form': needed_item_formset})
The template then might look something like this.
<form>
{% machine_service_form.as_p %}
{% needed_item_formset.as_p %}
</form>
(I didn't test this, so I don't give guarantees it works, but I think you should have enough pointers to figure out which things you need).
When one would make a Machine_Service they would also need a form (or multiple) for the NeededItem model. That means you can basically ignore the Stock model when creating Machine_Services.
Related
I am trying to create a simple Django 2.2 application with single model, single model form + custom field and a simple CreateView. I am populating the choices dynamically based on a http call to a outside url. The dropdown is populated fine, but when I try to submit my form I am getting an error:
Select a valid choice. ... is not one of the available choices and the form is refreshed with new 3 suggestions in the dropdown.
models.py
class GhostUser(models.Model):
first_name = models.CharField("User's first name", max_length=100, blank=False)
last_name = models.CharField("User's last name", max_length=100, blank=False)
ghost_name = models.CharField("User's ghost name", max_length=100, blank=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return f"{self.ghost_name}"
def get_absolute_url(self):
return reverse('ghost_names:ghost-update', kwargs={'id': self.id})
views.py
class GhostCreateView(CreateView):
template_name = 'ghost_create.html'
form_class = GhostUserForm
success_url = '/'
# def get_context_data(self, **kwargs):
# data = super().get_context_data(**kwargs)
# url = "https://donjon.bin.sh/name/rpc-name.fcgi?type=Halfling+Male&n=3"
# resp = urllib.request.urlopen(url)
# names = resp.read().decode('utf-8')
# data['ghost_suggestions'] = names.splitlines()
# return data
forms.py
class GhostUserForm(forms.ModelForm):
ghost_name = forms.ChoiceField(choices=[], widget=forms.Select())
class Meta:
model = GhostUser
fields = ['first_name', 'last_name']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['ghost_name'].choices = tuple(get_ghost_names())
def get_ghost_names():
url = "https://donjon.bin.sh/name/rpc-name.fcgi?type=Halfling+Male&n=10"
resp = urllib.request.urlopen(url)
data = resp.read().decode('utf-8').splitlines()
names = []
for name in data:
existing_ghosts = GhostUser.objects.filter(ghost_name=name)
if existing_ghosts:
continue
else:
print(name.split())
if len(name.split()) > 1:
name = name.split()[0]
names.append((name, name))
return names[:3]
html
{% block content %}
<form action="." method="POST">{% csrf_token %}
{% for field in form.visible_fields %}
<p>
{{ field.label_tag }}
{{ field }}
{{ field.errors }}
</p>
{% endfor %}
<input type="submit" value="Create ghost name">
</form>
{% comment %}{{ghost_suggestions}}
<select name="prefer_car_model" id="id_prefer_car_model" required>
<option value="0" selected disabled> Select ghost name </option>
{% for obj in ghost_suggestions %}
<option value="{{ obj }}">{{ obj }} </option>
{% endfor %}
</select>
{% endcomment %}
{% endblock content %}
What am I doing wrong here? I would appreciate your help on this weird for me issue.
P.S. When I add the commented out code from the view and template and render the fold fields one by one, the form submits without errors.
you did not mention the field name ghost_name in the form.py GhostUserForm class
change the line
fields = ['first_name', 'last_name'] to fields = ['first_name', 'last_name','ghost_name ']
I've looked through every similar question (and tried them), but still couldn't find answer.
I have two models:
class Project(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
name = models.CharField(max_length=120, verbose_name = "Название проекта")
url = models.URLField(max_length=120, unique=True, verbose_name = "Полный адрес сайта")
robots_length = models.CharField(max_length=5, default=0)
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
def __unicode__(self):
return self.name
def __str__(self):
return self.name
def get_absolute_url(self):
from django.urls import reverse
return reverse('projects:detail', args=[str(self.id)])
class ProjectPage(models.Model):
page_project = models.ForeignKey(Project, on_delete=models.CASCADE)
page_url = models.URLField(verbose_name = "Адрес страницы")
page_title = models.CharField(max_length=300, blank=True, verbose_name = "meta-title",default="")
page_description = models.CharField(max_length=300, blank=True, verbose_name = "meta-description",default="")
page_h1 = models.CharField(max_length=300, blank=True, verbose_name = "Заголовок h1",default="")
def __unicode__(self):
return self.page_url
def __str__(self):
return self.page_url
For each model there is a form:
class ProjectFormUpdate(forms.ModelForm):
class Meta:
model = Project
fields = [
"name",
"url",
]
widgets = {
'name': forms.TextInput(attrs={'placeholder': 'Произвольное название'}),
}
class ProjectPageForm(forms.ModelForm):
class Meta:
model = ProjectPage
fields = [
'page_project',
'page_url',
'page_title',
'page_description',
'page_h1',
]
widgets = {
'page_project': forms.HiddenInput()
}
In views.py I have:
def projects_update(request, proj=None):
instance = get_object_or_404(Project, id=proj)
form = ProjectFormUpdate(request.POST or None, instance=instance)
formset_f = modelformset_factory(ProjectPage, form=ProjectPageForm, extra=3)
formset = formset_f(queryset=ProjectPage.objects.filter(page_project__id=proj), initial =[{'page_project': proj}])
if request.method == 'POST':
formset = formset_f(request.POST)
for formset_form in formset:
if formset_form.is_valid() and formset_form.has_changed():
formset_form.save()
if form.is_valid():
form.save()
context = {
'title': "Редактируем проект - "+instance.name,
'form': form,
'formset': formset,
'instance': instance,
}
return render(request, "projects_update.html", context)
And, finaly, html
<form method="POST" action="" class="create-form">
{{ formset.management_form }}
{% csrf_token %}
<div class="row">
<div class="col-lg-6 offset-lg-3 col-md-10 offset-md-1 col-xs-10 offset-xs-1 form-bg">
<h2>Общие данные</h2>
{{ form|crispy}}
<input type="submit" class="btn btn-success" value="Обновить проект" />
</div>
</div>
{% for formset_form in formset %}
<div class="row form-container">
<div class="col-lg-6 offset-lg-3 col-md-10 offset-md-1 col-xs-10 offset-xs-1 form-bg">
<h3>Страница {{forloop.counter}}</h3>
{{ formset_form|crispy}}
</div>
</div>
{% endfor %}
</form>
What I am trying to achieve is: when user enters a page, he gets a form with project name and project URL already filled in. So, he can correct them.
Below, I want to show a filled in form for every page allready created for this project and several empty forms for creating new.
What happens is all initial data is displayed correctly, but when I fill several empty forms - only first empty form is saved each time.
Here is how it was solved:
Included errors properly.
Saw that second to last form lack required field (hiddenInput)
Made changes in view so it looks like:
formset_f = modelformset_factory(ProjectPage, form=ProjectPageForm, extra=3)
formset = formset_f(queryset=ProjectPage.objects.filter(page_project__id=proj), initial =[{'page_project': proj}, {'page_project': proj}, {'page_project': proj}])
Initial values now match number of extra forms - every form got it's own foreign key.
Probably there is a better solution, but the the problem is found and solved for me!
My problem was that when I tried to render every form of the formset manually I added an unneded <form></form> html element
wrong:
{ form.management_form }}
{% for form in formset %}
<form class="form-class">
{{form.name}}
</form>
right:
{ form.management_form }}
{% for form in formset %}
<div class="form-class">
{{form.name}}
</div>
After that change my forms were recognized correctly.
EDIT: Added model classes at the bottom.
I have this view code that takes input from a Django form and uses it to search the database for values.
class SearchResults(generic.FormView):
template_name = 'search_results.html'
context_object_name = 'submissions'
form_class = SearchForm
model = Submissions
def get_context_data(self, **kwargs):
context = super(SearchResults, self).get_context_data(**kwargs)
level = self.request.GET['level']
media_type = self.request.GET['media_type']
tags = MajorTags.objects.filter(
tag__icontains=self.request.GET['search'])
tag_ids = [t.id for t in tags]
context['results'] = Submissions.objects.filter(
tags__id__in=tag_ids,
level__contains=level,
media_type__contains=media_type)
return context
That then ends up being rendered by this template.
{% extends "base.html" %}
{% block content %}
<form action="{% url 'search_results' %}" method="get" class="global-search">
{% csrf_token %}
{{ form }}.
<input type="submit" value="Go" />
</form>
<ul>
{% for resource in results %}
<li> {{resource.url}} {{resource.stats_set}}
</li>
{% endfor %}
</ul>
{% endblock %}
The problem is that {{resource.stats_set}} returns nothing in the template. (The exact output is doc_sub.Stats.None.) However, when I run the following code in the shell, I get the result I'm looking for.
from doc_sub.models import MajorTags, Submissions
tags = MajorTags.objects.filter(tag__icontains='Theology')
tag_ids = [t.id for t in tags]
level = 'Introductory'
media_type='HTML'
context = Submissions.objects.filter(tags__id__in=tag_ids, level__contains=level, media_type__contains=media_type)
for i in context:
print i.stats_set.values()
Here are the necessary models:
class Submissions(models.Model):
LEVEL = (
('Introductory', 'Introductory'),
('Intermediate', 'Intermediate'),
('Academic', 'Academic'),
)
MEDIA_TYPE = (
('HTML', 'HTML'),
('PDF', 'PDF'),
('Video', 'Video'),
('Other', 'Other'),
)
id = models.AutoField(primary_key=True)
url = models.URLField(unique=True)
tags = models.ForeignKey(MajorTags, default=0)
level = models.CharField(choices=LEVEL, max_length=25)
media_type = models.CharField(choices=MEDIA_TYPE, max_length=25)
def __unicode__(self):
return self.url
class Stats(models.Model):
url = models.ForeignKey(Submissions)
id = models.AutoField(primary_key=True)
avg = models.FloatField(default=0)
std_dev = models.FloatField(default=0)
def __unicode__(self):
return self.url.url + " " + str(self.avg)
Well, I figured it out. If I had been smart and RTFM I may have figured it out sooner, however, I was trying to access the value the wrong way. I was doing this: print t.stats_set.values()[0]['avg'] like I was accessing values in a normal python context, but I needed to be doing this: resource.stats_set.values.0.avg
Im new in Django, and Im trying to create a form for add books for my app. But I want the date of publication not included in the form. Instead I want the current system date is obtained and will " add" the form to save my model . How could I do this?
There is part of my views.py:
def add_book(request):
if request.method == 'POST':
form = BookForm(request.POST)
if form.is_valid():
new_book = form.save(commit=False)
new_book.publication_date = django_timezone
new_book.save()
return HttpResponseRedirect('/thanks/')
else:
print form.errors
else:
form = BookForm()
return render_to_response('add_book.html',{'form':form})
There is my forms.py:
class BookForm(ModelForm):
class Meta:
model = Book
exclude = ('publication_date',)
And my model Book:
class Book(models.Model):
title = models.CharField(max_length = 100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
num_pages = models.IntegerField(blank = True, null = True)
class Admin:
list_display = ('title', 'publisher', 'publication_date')
list_filter = ('publisher', 'publication_date')
ordering = ('-publication_date',)
search_fields = ('title',)
def __str__(self):
return self.title
I used this template for form:
{% extends 'base.html' %}
{% block title %}Add a new Book{% endblock %}
{% block content %}
<h3> Here you can add a new book into the local DataBase </h3>
<form action="." method="post">{% csrf_token %}>
<div class="fieldWrapper">
{{ form.title.errors }}
<label for="id_title">Book Title</label>
{{ form.title }}
</div>
<div class="fieldWrapper">
{{ form.authors.errors }}
<label for="id_authors">Authores</label>
{{ form.authors }}
</div>
<div class="fieldWrapper">
{{ form.publisher.errors }}
<label for="id_publisher">Publishers</label>
{{ form.publisher }}
</div>
<div class="fieldWrapper">
{{ form.num_pages.errors }}
<label for="id_num_pages">Number of Pages</label>
{{ form.num_pages }}
</div>
<p><input type="submit" value="Submit"></p>
</form>
{% endblock %}
I've temporarily disabled Django csrf because I do not need for my purpose
To do that, you need to pass the default argument as timezone.now in publication_date model field.
models.py
class Book(models.Model):
title = models.CharField(max_length = 100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
# pass the default argument in publication_date field
publication_date = models.DateField(default=timezone.now)
num_pages = models.IntegerField(blank = True, null = True)
class Admin:
list_display = ('title', 'publisher', 'publication_date')
list_filter = ('publisher', 'publication_date')
ordering = ('-publication_date',)
search_fields = ('title',)
def __str__(self):
return self.title
views.py
After doing that, you can directly call .save() on modelform. Now, the book object will be created with the aware datetime.
def add_book(request):
if request.method == 'POST':
form = BookForm(request.POST)
if form.is_valid():
new_book = form.save() # directly save the book object
return HttpResponseRedirect('/thanks/')
else:
print form.errors
else:
form = BookForm()
return render_to_response('add_book.html',{'form':form})
Why we did not use auto_now_add argument in the model field?
From django's auto_now_add documentation:
Django's auto_now_add uses current time which is not timezone aware. So, it is better to explicitly specify default value using default argument.
If you want to be able to modify this field, set default=timezone.now
(from django.utils.timezone.now()) instead of auto_now_add=True.
I would like to fill my edit_post.html with the values already stored in the database .
My edit url is http://hostname/edit_Post/960 . "960" corresponds to the item id in my database . I request with that ID to update its contents.
I wann the contents to appear here :-
edit_Post.html
<form id="category_form" method="post" action="/edit_Post/">
{% csrf_token %}
{% for field in form.visible_fields %}
{{ field.errors }}
<input type="text" placeholder='{{ field.help_text }}'>
{% endfor %}
<input type="submit" name="submit" value="Create Post" />
</form>
urls.py
url(r'^edit_Post/(?P<id>\d+)', 'blog.views.edit_Post'),
forms.py
class addPostForm(forms.ModelForm):
title = forms.CharField(max_length=128, help_text="Please enter the title ")
author = forms.CharField(max_length=128, help_text="Please enter the Author ")
bodytext = forms.CharField(max_length=128,
help_text="Please enter the Body",
required=False)
# An inline class to provide additional information on the form.
class Meta:
# Provide an association between the ModelForm and a model
model=posts
fields = ('title', 'author','bodytext')
finally views.py
def edit_Post(request, id):
context = RequestContext(request)
instance=posts.objects.get(id=id)
form = addPostForm(instance=instance)
if request.method == "POST":
form = addPostForm(request.POST,instance=instance)
if form.is_valid():
form.save(commit=True)
confirmation_message = "Post information updated successfully!"
return HttpResponseRedirect('/home')
else:
print form.errors
else:
form=addPostForm(instance=instance)
return render_to_response('edit_Post.html', {'form': form}, context)
my model.py
class posts(models.Model):
author = models.CharField(max_length = 30)
title = models.CharField(max_length = 100)
bodytext = models.TextField()
timestamp = models.DateTimeField(default=datetime.now, blank=True)
Django would already do this, if you hadn't bypassed its mechanism of outputting fields. In your template you should do this:
{% for field in form.visible_fields %}
{{ field.errors }}
{{ field }}
{% endfor %}
If you want to keep the placeholder functionality, add that in the form class itself:
class addPostForm(forms.ModelForm):
title = forms.CharField(max_length=128, widget=forms.TextInput(attrs={'placeholder': 'Please enter the title'}))
Django provides CRUD(Create -> CreateView, Remove ->DeleteView, Update -> UpdateView, Details -> DetailView) Views. If you can, use them.
You can change your method from views.py in a class.
class PostDetail(DetailView):
model = posts
form_class = addPostForm
template = "edit_post.html"
Then you can add methods there :).