Django form missing a field - python

I have a model and a modelform to change some settings. The form is displayed allright, with the correct values, but when I submit the form one field is missing in the request.POST dict.
The model:
class NodeSettings(models.Model):
nodetype = models.CharField(max_length=8, editable=False)
nodeserial = models.IntegerField(editable=False)
upper_limit = models.FloatField(null=True, blank=True,
help_text="Values above this limit will be of different color.")
graph_time = models.IntegerField(null=True, blank=True,
help_text="The `width' of the graph, in minutes.")
tick_time = models.IntegerField(null=True, blank=True,
help_text="Number of minutes between `ticks' in the graph.")
graph_height = models.IntegerField(null=True, blank=True,
help_text="The top value of the graphs Y-axis.")
class Meta:
unique_together = ("nodetype", "nodeserial")
The view class (I'm using Django 1.3 with class-based views):
class EditNodeView(TemplateView):
template_name = 'live/editnode.html'
class NodeSettingsForm(forms.ModelForm):
class Meta:
model = NodeSettings
# Some stuff cut out
def post(self, request, *args, **kwargs):
nodetype = request.POST['nodetype']
nodeserial = request.POST['nodeserial']
# 'logger' is a Django logger instance defined in the settings
logger.debug('nodetype = %r' % nodetype)
logger.debug('nodeserial = %r' % nodeserial)
try:
instance = NodeSettings.objects.get(nodetype=nodetype, nodeserial=nodeserial)
logger.debug('have existing instance')
except NodeSettings.DoesNotExist:
instance = NodeSettings(nodetype=nodetype, nodeserial=nodeserial)
logger.debug('creating new instance')
logger.debug('instance.tick_time = %r' % instance.tick_time)
try:
logger.debug('POST[tick_time] = %r' % request.POST['tick_time'])
except Exception, e:
logger.debug('error: %r' % e)
form = EditNodeView.NodeSettingsForm(request.POST, instance=instance)
if form.is_valid():
from django.http import HttpResponse
form.save()
return HttpResponse()
else:
return super(EditNodeView, self).get(request, *args, **kwargs)
The relevant portion of the template:
<form action="{{ url }}edit_node/" method="POST">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Ok" />
</form>
Here is the debug output in the console when running the debug server:
2011-04-12 16:18:05,972 DEBUG nodetype = u'V10'
2011-04-12 16:18:05,972 DEBUG nodeserial = u'4711'
2011-04-12 16:18:06,038 DEBUG have existing instance
2011-04-12 16:18:06,038 DEBUG instance.tick_time = 5
2011-04-12 16:18:06,039 DEBUG error: MultiValueDictKeyError("Key 'tick_time' not found in <QueryDict: {u'nodetype': [u'V10'], u'graph_time': [u'5'], u'upper_limit': [u''], u'nodeserial': [u'4711'], u'csrfmiddlewaretoken': [u'fb11c9660ed5f51bcf0fa39f71e01c92'], u'graph_height': [u'25']}>",)
As you can see, the field tick_time is nowhere in the QueryDict from request.POST.
It should be noted that the field is in the web-browser, and when looking at the HTML source it looks just like the other fields in the form.
Anyone have any hints on what can be wrong?

Since you are using generic view, isn't it best to extend ProcessFormView instead of TemplateView?
EDIT
I've tried your code with TemplateView:
class EditNodeView(TemplateView):
Do you have a get_context_data to push the form?
def get_context_data(self, **kwargs):
instance = NodeSettings.objects.get(pk=kwargs['node_id'])
form = EditNodeView.NodeSettingsForm(instance=instance)
context = super(EditNodeView, self).get_context_data(**kwargs)
context['form'] = form
return context
The best way for editing existing objects is to get by primary key, I have the following in the urls.py:
url(r'^edit-node/(?P<node_id>\d+)/$', EditNodeView.as_view(), name='edit-node'),
I fetch the instance by primary key, might want to do some checking above like throwing 404 if not present.
In your models, you have nodetype and nodeserial as editable=False, how do you display or create those items if they are set to not editable? I've set them to True for testing purposes.
In the template, I have changed the first line to:
<form action="" method="POST">
I'm aware that there are a lot of changes but the above can view and edit your model properly. You could set the nodetype and nodeserial to be read-only at the form level to prevent people from editing it.

Related

Django Form Dynamic Fields looping over each field from POST and creating records

I'm looking for some advice where to go from here. I've been working on making a Form, which dynamically generates its fields.
The form is working and generating everything correctly. However, I am having issues with how to save the actual form data. I'm looking for each field to save as a new item in a model.
The View Class from view.py
class MaintenanceCheckListForm(LoginRequiredMixin, FormView):
login_url = '/accounts/login'
template_name = 'maintenance/checklist.html'
form_class = MaintenanceCheckListForm
success_url = reverse_lazy('m-checklist')
def form_valid(self, form):
form.cleaned_data
for key, values in form:
MaintenanceCheckList.objects.create(
item = key,
is_compliant = values
)
return super().form_valid(form)
The Form from forms.py
class MaintenanceCheckListForm(forms.Form):
def __init__(self, *args, **kwargs):
super(MaintenanceCheckListForm, self).__init__(*args, **kwargs)
items = Maintenance_Item.objects.all()
CHOICES = (
('P','Compliant'),
('F','Non-Compliant'),
)
for item in items:
self.fields[str(item.name)] = forms.ChoiceField(
label=item.name,
choices=CHOICES,
widget=forms.RadioSelect,
initial='F',
)
The Model, from models.py
class MaintenanceCheckList(CommonInfo):
CHOICES = (
('P','Compliant'),
('F','Non-Compliant'),
)
id = models.AutoField(primary_key=True)
item = models.CharField(max_length=100)
is_compliant = models.CharField(max_length=20, choices= CHOICES)
I am having trouble accessing the data from the Form when it POST's. I've done some troubleshooting where I have set the values statically in the '''form_valid''' and it appears to generate the correct amounts of entires in the model. However the trouble begins when I attempt to insert the values from the POST.
I receieve the below error, which I believe it is trying to dump all the keys and values into a single item instead of looping over each key, value and creating the item.
DataError at /maintenance/checklist
value too long for type character varying(100)
Request Method: POST
Request URL: http://t1.localhost:8000/maintenance/checklist
Django Version: 3.1.6
Exception Type: DataError
Exception Value:
value too long for type character varying(100)
I'm fairly new to the world of Django (4 weeks and counting so far, and maybe 12 weeks into python). So any assistance would be amazing!
I believe you have somewhat gone on a tangent. There's a simpler solution of using Model formsets for what you want.
First if you want a custom form make that:
from django import forms
class MaintenanceCheckListComplianceForm(forms.ModelForm):
item = forms.CharField(widget = forms.HiddenInput())
is_compliant = forms.ChoiceField(
choices=MaintenanceCheckList.CHOICES,
widget=forms.RadioSelect,
initial='F',
)
class Meta:
model = MaintenanceCheckList
fields = ('item', 'is_compliant')
Next use it along with modelformset_factory in your views:
from django.forms import modelformset_factory
class MaintenanceCheckListFormView(LoginRequiredMixin, FormView): # Changed view name was a bit misleading
login_url = '/accounts/login'
template_name = 'maintenance/checklist.html'
success_url = reverse_lazy('m-checklist')
def form_valid(self, form):
instances = form.save()
return super().form_valid(form)
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['queryset'] = MaintenanceCheckList.objects.none()
kwargs['initial'] = [{'item': obj['name'], 'is_compliant': 'F'} for obj in Maintenance_Item.objects.all().values('name')]
return kwargs
def get_form(self, form_class=None):
kwargs = self.get_form_kwargs()
extra = len(kwargs['initial'])
form_class = modelformset_factory(MaintenanceCheckList, form=MaintenanceCheckListComplianceForm, extra=extra)
return form_class(**kwargs)
Now in your template:
<form method="post">
{{ form }}
</form>
Or manually render it:
<form method="post">
{{ form.management_form }}
{% for sub_form in form %}
Item: {{ sub_form.item.value }}
{{ sub_form }}
{% endfor %}
</form>
Note: The above usage is a bit weird due to the naming of the formset variable as form by the FormView you should look into improving that a bit.
Note: Looking at the implementation it feels a bit weird to do this. I would advice you to redesign your models a bit. Perhaps a foreign key between your models? It basically feels like you have duplicate data with this implementation.

Issue with Django form POST method

I am having a problem with the following view and form. The form loads correctly however when I edit any of the fields it does not save. After a bit of debugging I think it is due to one of two things: either request.method == "POST" is evaluating to false, or form.is_valid() is evaluating to false. So potentially something wrong with my template or my clean() method? I've searched previous questions and can't find anything that helps. I've also checked my clean() method against the Django docs and think it is OK.
views.py
#login_required
def edit_transaction(request, pk):
transaction = get_object_or_404(Transaction, pk=pk)
if request.method == "POST":
form = TransactionForm(request.POST, instance=transaction)
if form.is_valid():
transaction = form.save(commit=False)
transaction.updated = timezone.now()
transaction.save()
return redirect('view_transaction_detail', pk=transaction.pk)
else:
form = TransactionForm(request=request, instance=transaction)
return render(request, 'budget/new_transaction.html', {'form': form})
forms.py
class TransactionForm(forms.ModelForm):
class Meta:
model = Transaction
fields = ('title', 'transaction_type', 'category', 'budgeted_amount', 'actual_amount', 'date', 'comments',)
#new_category field to allow you to add a new category
new_category = forms.CharField(max_length=30, required=False, label="New Category Title")
def __init__(self, request, *args, **kwargs):
super(TransactionForm, self).__init__(*args, **kwargs)
#category is now not a required field because you will use category OR new_category
self.fields['category'].required=False
#set to allow use of self.request.user to set user for category
self.request = request
def clean(self):
category = self.cleaned_data.get('category')
new_category = self.cleaned_data.get('new_category')
if not category and not new_category:
# raise an error if neither a category is selected nor a new category is entered
raise forms.ValidationError('Category or New category field is required')
elif not category:
# create category from new_category
category, created = Category.objects.get_or_create(title=new_category, defaults={'user': self.request.user})
self.cleaned_data['category'] = category
return super(TransactionForm, self).clean()
template
{% extends 'budget/base.html' %}
{% block content %}
<h2>New transaction</h2>
<h4>To add a new category, leave Category blank and enter your new category in the New Category Title field</h4>
<form method="POST" class="post-form">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default">Save</button>
</form>
{% endblock %}
update following answer - accessing request through kwargs
def __init__(self, *args, **kwargs):
super(TransactionForm, self).__init__(*args, **kwargs)
self.fields['category'].required=False
self.request = kwargs.pop('request', None)
As I mentioned on your last question, since you've changed the signature of the form's init method you need to pass the request both times you instantiate it. You're only doing so when it is not POST; so, when it is a POST, Python takes the data that you passing and assigns it to the request argument, leaving the data itself blank.
form = TransactionForm(request, data=request.POST, instance=transaction)
Note it is precisely for this reason that is is a bad idea to change the signature; instead, pass request as a keyword argument and inside the method get it from kwargs.

Blank Result When Filtering ForeignKey values in Django ModelchoiceField

I have three models and they serve as 'Foreignkey' to each other.
Hotel models
class Hotel(model.Models):
user= models.ForeignKey(User)
name= models.CharField(max_length=100, verbose_name='hotel_name')
address= models.CharField(max_length=100, verbose_name='hotel_address')
#more fields
Rooms models
class HotelRooms(models.Model):
hotel= models.ForeignKey(Hotel, related_name='myhotels')
slug=models.SlugField(max_length=100, unique=True)
room_name=models.CharField(max_length=100, verbose_name='Room Name')
#other fields
HotelCalendar models
class HotelCalendar(models.Model):
user=models.ForeignKey(User)
hotel=models.ForeignKey(Hotel)
hotelrooms=models.ForeignKey(HotelRooms)
#other fields
Now, I want to display all rooms that belongs to a hotel in HotelCalender form in order for the owner to select the room he/she wants to update and save.
HotelCalendar form
class HotelCalendarForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(HotelCalendarForm, self).__init__(*args, **kwargs)
self.fields['hotelrooms'].queryset= HotelRooms.objects.filter(hotel=self.instance.hotel_id)
Template
<form id="post_form" method="post" action=""
enctype="multipart/form-data">
{% csrf_token %}
{{ HotelCalendarForm.as_p }}
<input type="submit" name="submit" value="Submit" />
</form>
Views
def hotel_calendar_view(request, hotel_id):
if request.method=="POST":
form=HotelCalendarForm(request.POST)
if form.is_valid():
data=form.cleaned_data
newbookdate=HotelCalendar(
user=request.user,
hotel=Hotel.objects.get(id=hotel_id),
hotelrooms=data['hotelrooms'],)
newbookdate.save()
return render(request, 'notice.html')
#other code here
When I load the form, it won't return any value, The modelchoicefield is just blank.
What am I missing?
It seems you are trying to populate the hotelrooms field with the filtered results by the hotel when the form is loaded on a GET request. If that's the case the field wont load any data as the instance variable will be None.
For the initial data you need to pass the hotel id to the form when it is being initialized on the GET request and in the form, load the data using the hotel id and not instance.hotel_id. For example:
views.py
#login_required
def hotel_calendar_view(request, hotel_id):
if request.method=="POST":
## code to post the form data
else:
context = {
'HotelCalendarForm’: HotelCalendarForm(hotel_id=hotel_id),
'hotel': Hotel.objects.get(id=hotel_id)
}
return render(request, 'hotels/hotel_calendar.html', context)
# return default response
and then in your forms:
forms.py
class HotelCalendarForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
hotel_id = kwargs.pop(“hotel_id”, None)
super(HotelCalendarForm, self).__init__(*args, **kwargs)
if hotel_id:
self.fields['hotelrooms'].queryset=HotelRooms.objects.filter(hotel=hotel_id)
You should modify the line
self.fields['hotelrooms'].queryset = HotelRooms.objects.filter(hotel=self.instance.hotel_id)
to
self.fields['hotelrooms'].queryset = HotelRooms.objects.filter(hotel=self.instance.hotel)
When you are filtering on foreign key it expects a model instance. If you would want to filter on foreign key you would have to do it like this:
HotelRooms.objects.filter(hotel_id=self.instance.hotel_id)
If you want to know more read https://docs.djangoproject.com/ja/1.9/topics/db/queries/#field-lookups

Pass choices from views to form

I use the form Bannerform to create new Banner object trough the add_banner views (and template). I use the class Options to definite which affiliation objects to permit in the form Bannerform (affiliation field).
I'm trying to pass choices from the views to the form but it give me ValueError: too many values to unpack (expected 2)
My code worked when new_affiliation was a ForeignKey but now I need more values. I think I must definite 'choices' in the views or I will have problem on the first migration (it appear to call the database tables from the models.py but not from the views.py, so if I put Options.objects.get(id=1) on the models.py it give error because the tables don't exist yet).
My form.py:
from django import forms
from core.models import Options, Banner, Affiliation #somethings other
class BannerForm(forms.ModelForm):
name = forms.CharField(max_length=32)
affiliation = forms.ChoiceField('choices')
#affiliation = forms.ModelChoiceField('choices') #same error
class Meta:
model = Banner
exclude = (#some fields)
My models.py:
from django.db import models
from django.contrib.auth.models import User
from django import forms
class Options(models.Model):
new_affiliation = models.ManyToManyField('Affiliation')
#new_affiliation = models.ForeignKey('Affiliation') #this worked (with some changes in views)
class Affiliation(models.Model):
name = models.CharField(max_length=32, unique=True)
class Banner(models.Model):
name = models.CharField(max_length=32, unique=True)
affiliation = models.ForeignKey(Affiliation)
My views.py:
def add_banner(request):
if request.method == 'POST':
#some code here
else:
options = Options.objects.get(id=1)
print(options.new_affiliation.all()) #controll
choices = options.new_affiliation.all()
print(choices) #controll
form = BannerForm(choices, initial={
#some code regarding other fields
})
return render(request, 'core/add_banner.html', {'form': form})
My add_banner.html:
<form role="form" id="banner_form" enctype="multipart/form-data "method="post" action="../add_banner/">
{% csrf_token %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form.visible_fields %}
{{ field.errors }}
{{ field.label }}
{{ field }}
{{ field.help_text }}
<br />
{% endfor %}
Any help will be apreciated.
Updated. I changed only views.py:
def add_banner(request):
if request.method == 'POST':
#some code here
else:
options = Options.objects.get(id=1)
print(options.new_affiliation.all()) #controll
choices = tuple(options.new_affiliation.all())
print(choices) #controll
form = BannerForm(choices, initial={
#some code regarding other fields
})
return render(request, 'core/add_banner.html', {'form': form})
But still give error.
Update 2. If I pass choices directly from form.py it works:
My views.py:
def add_banner(request):
if request.method == 'POST':
#some code here
else:
form = BannerForm(request.POST or None, initial={
#some code regarding other fields
})
return render(request, 'core/add_banner.html', {'form': form})
My forms.py:
class BannerForm(forms.ModelForm):
options = Options.objects.get(id=1)
choices = options.new_affiliation.all()
name = forms.CharField(max_length=32)
affiliation = forms.ModelChoiceField(choices)
Unluckly this give problems on the first migration (see above).
I'm trying to pass choices using some init method...
My forms.py:
class BannerForm(forms.ModelForm):
name = forms.CharField(max_length=32)
affiliation = forms.ModelChoiceField(choices)
def __init__(self, *args, **kwargs):
options = Options.objects.get(id=1)
choices = options.new_affiliation.all()
#choices = kwargs.pop('choices')
super(RegentForm, self).__init__(*args, **kwargs)
self.fields['affiliation'] = choices
but it say that choices is not definite
From what I can see here, it looks like you are getting an error "too many values to unpack" because you are not sending "choices" as the correct type. A ChoiceField takes choices only as a tuple, as seen in the documentation for models. If you are looking to define choices based on a QuerySet, you'll have to convert it into a tuple which can be interpreted as valid "choices". In one of my projects for example, I needed to prepare a set of years as a tuple so that I could allow users to select from a list of pre-determined years. I specified the following function to do this:
def years():
response = []
now = datetime.utcnow()
for i in range(1900, now.year + 1):
response.append([i, str(i)])
return tuple(response)
Since tuples are meant to be immutable, it usually isn't a good idea to cast them, just based on principle. However, in this case it seems necessary as a measure to declare that you are okay with the possible variability with these statements.
In your specific situation, you might consider doing something like this:
choices = tuple(options.new_affiliation.all().values())
I have not tested this code, and I frankly am not completely familiar with your project and may be mistaken in some part of this response. As a result, it may require further tweaking, but give it a try. Based on your error, this is definitely where the program is breaking currently. Update here if you make any progress.
Done!
My form.py:
class BannerForm(forms.ModelForm):
name = forms.CharField(max_length=32, label='Nome')
def __init__(self, *args, **kwargs):
options = Options.objects.get(id=1)
choices = options.new_affiliation.all()
super(BannerForm, self).__init__(*args, **kwargs)
self.fields['affiliation'] = forms.ModelChoiceField(choices)
self.fields['affiliation'].initial = choices
class Meta:
model = Banner
My views.py:
def add_banner(request):
if request.method == 'POST':
#some code here
else:
form = BannerForm(request.POST or None, initial={
#some code here
})
return render(request, 'core/add_banner.html', {'form': form})
Thank you

Filter ForeignKey id From Django Models

I want to get data from some fields in a Foriegnkey that belongs to a particular id. But I can;t figure out how to make it work. I have two models;
class Tick(models.Model):
user=models.ForeignKey(User)
event_name=models.CharField(max_length=100)
ticket_quantity=models.PositiveIntegerField(max_length=50, null=True, blank=True,help_text='Optional, if you have unlimited ticket')
ticket_plan_name_a=models.CharField(max_length=100, null=True, blank=True)
ticket_plan_price_a=models.PositiveIntegerField(max_length=50, null=True, blank=True, verbose_name="Price",help_text='ticket price.')
ticket_plan_name_b=models.CharField(max_length=100, null=True, blank=True)
ticket_plan_price_b=models.PositiveIntegerField(max_length=50, null=True, blank=True, verbose_name="Price1",help_text='ticket price.')
Another Models
class BuyTick(models.Model):
user=models.ForeignKey(User)
tik=models.ForeignKey(Tick)
tiket_qty=models.PositiveIntegerField(max_length=100)
pub_date=models.DateTimeField()
full_name=models.CharField(max_length=100)
def __unicode__(self):
return self.tiket
class BuyTickForm(forms.ModelForm):
tik=forms.ModelChoiceField(queryset=Tick.objects.get(pk=tick_id))
class Meta:
model=BuyTick
After trying the above codes out I got
NameError: name 'tick_id' not defined
How can I get a particular tick_id from models Tick so as to display certain fields in the BuyTick form?
Update: For the views
def purchase_ticket(request, tikid):
if request.method=="POST":
form=BuyTickForm(request.POST)
if form.is_valid():
data=form.cleaned_data
newbuy=BuyTick(
user=request.user,
tik=Ticket.objects.get(pk=tikid),
tiket_qty=data['tiket_qty'],
full_name=data['full_name'],
phone_no=data['phone_no'],
pub_date=datetime.datetime.now())
newbuy.save()
return HttpResponse('Your ticket have been booked')
else:
print form.errors
else:
return render_to_response('buytickform.html',{'BuyTickForm':BuyTickForm,'post':Tick.objects.all().get(id=tikid)},context_instance=RequestContext(request))
Template
<p> Event Name: {{post.event_name}} </p>
<form action="." method="POST">
{% csrf_token %}
{{BuyTickForm.as_p}}
<input type="submit" class="radbutton radorange" value="Confirm Ticket"/>
</form>
First your model could use a couple of tweaks:
class BuyTick(models.Model):
. . .
tiket_qty=models.PositiveIntegerField(default=0, max_length=100)
pub_date=models.DateTimeField(auto_now_add=True)
. . .
def __unicode__(self):
# not sure what you're returning here...
return self.tiket
You need to pass in the id to filter on when you instantiate the form, and you can also pass in the user from the request, which you can set automatically:
class BuyTickForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.tikid = kwargs.pop('tikid')
self.user = kwargs.pop('user')
super(BuyTickForm, self).__init__(*args, **kwargs)
tik=forms.ModelChoiceField(queryset=Tick.objects.filter(id=self.tikid))
class Meta:
model=BuyTick
def save(self, commit=True):
buy_tick = super(BuyTickForm, self).save(commit=False)
buy_tick.user = self.user
if commit:
buy_tick.save()
return buy_tick
You need to filter() the Tick objects instead of using get, otherwise you'll be returning more than one object, which will raise an exception when using .get().
The view could use some love:
def purchase_ticket(request, tikid):
tick = get_object_or_404(Tick, id=tikid)
form = BuyTickForm(request.POST or None, tikid=tikid, user=request.user)
if request.method == 'POST' and form.is_valid():
form.save()
# note that these fields don't exist on your model:
# full_name, phone_no
# return something here... a redirect probably
else:
return render(request, 'buytickform.html',
{'tick': tick, 'form': form})

Categories