I'm trying to build a page for an inventory system that will allow a user to update a quantity of items received.
I want to show a table of all products and let the user enter the quantity received, which I'll post and iterate over to update the database.
Here is my view:
def new_shipment(request):
list_of_active_products = Product.objects.filter(status=1)
ShipmentFormSet = formset_factory(ShipmentForm, extra=0)
formset = ShipmentFormSet(initial=list_of_active_products)
return render_to_response('inventory/new_shipment.html', {'formset': formset})
Here's my model for the form:
class ShipmentForm(forms.Form):
sku = forms.IntegerField()
product_name = forms.CharField(max_length=100)
quantity = forms.IntegerField()
And here is the form template:
<form method="post" action="">
<table>
{% for form in formset %}
{{ form }}
{% endfor %}
</table>
<input type="submit" />
</form>
And here is the error I'm getting:
Caught AttributeError while rendering: 'Product' object has no attribute 'get'
Can anyone help me out with this?
From the docs it looks like you have to pass in a list of dictionaries as the initial data, rather than a QuerySet:
Also note that we are passing in a list of dictionaries as the initial data.
You may want to change your initial query to:
list_of_active_products = Product.objects.filter(status=1).values()
which will return a list of dictionaries rather than model-instance objects.
Using initial data with a formset:
https://docs.djangoproject.com/en/dev/topics/forms/formsets/#using-initial-data-with-a-formset
ValuesQuerySet:
https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.values
You can also use the queryset argument. This should work:
formset = ShipmentFormSet(queryset=list_of_active_products)
cf. https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#changing-the-queryset
Related
I am trying to display a ModelForm with prepopulated instance data.
It works fine except for the ChoiceField which always displays the first choice given in forms.py ('LP') rather than the choice provided by the instance.
View:
def review(request):
order = Order.objects.get(user__pk=request.user.id)
form = ProjectReviewForm(instance=order)
context = {
'form': form,
}
return render(request, 'users/projectreview.html', context)
Forms:
class ReviewForm(forms.ModelForm):
LAND = 'LP' // #INSTANCE ALWAYS SHOWS THIS RATHER THAN INSTANCE DATA
DATA = 'DC'
STATIC = 'SW'
CHOICES = (
(LAND, 'LP'),
(DATA, 'DC'),
(STATIC, 'SW')
)
product = forms.ChoiceField(choices=CHOICES, widget=forms.Select(attrs={'class': 'form-field w-input'}),)
class Meta:
model = Order
fields = '__all__'
template:
<form method="POST" class="contact-form">
{% csrf_token %}
<h2 class="form-heading-small">Please make sure everything you've submitted is correct.</h2>
{{ form }}
<button type="submit" data-wait="Please wait..." class="button w-button">Looks good!</button>
</form>
The product field on the form is overriding the field on the model. Look into using a ModelChoiceField
One of the fields returned to my template in a formset generated by modelformset_factory() is a JSONField. I'd like to parse the json and display elements in it separately within the form, read-only.
This is readily done if I just return the queryset list, using a builtin filter I created, get(). But it has to be in a form that has some added fields for users to fill.
{% for foo in foo_list %}
<h5>{{ foo.json|get:"title" }}</h5>
{% endfor %}
The problem is that doing the same for a formset field I get the error "'BoundField' object has no attribute 'get'"
{% for form in formset %}
<h5>{{ form.json|get:"title" }}</h5>
{% endfor %}
Here is the filter:
#register.filter(name='get')
def get(d, k):
print('get(d,k):',d,k)
return d.get(k, None)
The relevant bits of the view:
...
foo_list = Foo.objects.all()
...
FooFormset = modelformset_factory(
Foo, fields = ['task_id','authority','dataset','place_id',
'authrecord_id','json'], form=FooModelForm, extra=0)
formset = FooFormset(request.POST or None, queryset=foo_list)
context['formset'] = formset
...
return render(request, 'datasets/review.html', context=context)
I found that applying a custom filter to form.json.value did the trick. The new filter converts that (text) value to json, from which I can do a get for specific keys.
new get filter:
#register.filter(name='get')
def get(d, k):
jd = json.loads(d)
return jd.get(k, None)
and its implementation in a template:
{{ form.json.value|get:"names" }}
BoundField.value is text, even though json in the db, so it needs conversion back for parsing.
I'm trying to bind a dynamic list of choices to a ModelForm. The form is rendered correctly. However, when using the form with a POST Request, I get an empty form back. My goal is to save that form into the database (form.save()). Any help would be much appreciated.
Model
I'm using a multiple choice select field ( https://github.com/goinnn/django-multiselectfield )
from django.db import models
from multiselectfield import MultiSelectField
class VizInfoModel(models.Model):
tog = MultiSelectField()
vis = MultiSelectField()
Forms
class VizInfoForm(forms.ModelForm):
class Meta:
model = VizInfoModel
fields = '__all__'
def __init__(self,choice,*args,**kwargs):
super(VizInfoForm, self).__init__(*args,**kwargs)
self.fields['tog'].choices = choice
self.fields['vis'].choices = choice
View
Choices are passed from the view when instantiating the form.
def viz_details(request):
options = []
headers = request.session['headers']
for header in headers :
options.append((header, header))
if request.method == 'POST':
form = VizInfoForm(options, request.POST)
#doesnt' get into the if statement since form is empty!
#choices are not bounded to the model although the form is perfectly rendered
if form.is_valid():
form.save()
return HttpResponseRedirect('/upload')
else:
#this works just fine
form = VizInfoForm(options)
return render(request, 'uploads/details.html', {'form': form})
Template
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<p>Choose variables to toggle between</p>
{{ form.tog }}
<br></br>
<p>Choose variable to be visualized</p>
{{ form.vis }}
<br></br>
<button type="submit">Submit</button>
</form>
You're saying Django doesn't get into your if request.method == 'POST' block.
This tells us that you're not sending your request through the POST method. Your template probably has an error in it, maybe you haven't specified the method on your form, or you made your button to just be a link instead of a submit ?
Show your template so we can say more, unless this was enough to solve your question !
I have a form that I want to be pre-populated with data from a model instance when a user is attempting to update an existing database record. When the form is rendered none of the radio buttons pre-selected even though a model instance has been passed to the ModelForm. In a much larger form than listed below, all of the fields except the radio buttons are pre-populated with the correct data from the model instance. How do I get the correct radio buttons pre-selected?
My Models:
class TicketType(models.Model):
type = models.CharField(max_length=15, unique=True)
def __unicode__(self):
return self.type.title()
class TestTicket(models.Model):
ticket_type = models.ForeignKey(TicketType, to_field='type')
My Form
class TestTicketForm(ModelForm):
ticket_type = ModelChoiceField(TicketType.objects.all(),
widget=RadioSelect,
empty_label=None)
class Meta:
model = TestTicket
fields = ['ticket_type']
My View
def test_ticket_update(request, ticket_num=None):
# initialize an update flag to distinguish between a request
# to add a new ticket or an update to an existing ticket.
update_requested = False
ticket_instance = None
if ticket_num:
# This is a request to update a specific ticket.
# Attempt to retrieve the ticket or show a 404 error.
# If a ticket is retrieved, it is locked for editing
# by using 'select_for_update()' to prevent a race condition.
ticket_instance = get_object_or_404(
TestTicket.objects.select_for_update(),pk=ticket_num)
update_requested = True
if request.method == 'POST':
form = TestTicketForm(request.POST, instance=ticket_instance)
if form.is_valid():
ticket = form.save(commit=False)
ticket.save()
return HttpResponseRedirect('/tickets/')
else:
if update_requested:
# This is a requested to update an existing ticket.
# Bind the ticket data to a form.
form = TestTicketForm(instance=ticket_instance)
else:
form = TestTicketForm()
return render(request, 'ticket_tracker/ticket_update.html',
{ 'form': form, 'ticket': ticket_instance})
My Template
{% block content %}
<div class="container">
<form action="/tickets/test-ticket/{{ ticket.id }}/" method="post">
{% csrf_token %}
{{ form.as_p }}
<div class="form-group">
<button type="submit">Save</button>
</div>
</form>
</div>
{% endblock content %}
It appears this has been answered here before. In my model I used the to_field argument in the creation of the ForeignKey field, but the ModelChoiceField is expecting to use the id when the 'initial' value is passed to it. There are several options to fix this in my example including:
Remove the to_field parameter from the ForeignKey field in the model.
When creating the form instance in the view, set the 'initial' parameter for the field using the field's id from the model instance, e.g.,
form = TestTicketForm(request.POST,
instance=ticket_instance,
initial={'ticket_type': instance.ticket_type.id)
Set the form field's initial value in the forms __init__() method. Again this uses the field's id from the model instance. For example:
class TestTicketForm(ModelForm):
ticket_type = ModelChoiceField(TicketType.objects.all(),
widget=RadioSelect,
empty_label=None)
def __init__(self, *args, **kwargs):
super(TestTicketForm, self).__init__(*args, **kwargs)
if self.instance is not None:
self.initial['ticket_type'] = self.instance.ticket_type.id
Option #1 above would require a schema and data migrations in my database. Options #2 and #3 are similar but I chose option #3 since it makes my view code slightly cleaner.
I have an Item object that has a manytomany relation to another object Option. I create a modelform from the Item object like so;
class Item(models.Model):
category = models.ForeignKey(Category)
name = models.CharField(max_length=200)
price = models.DecimalField(max_digits=9, decimal_places=2, blank=True, null=True)
options = models.ManyToManyField(Option)
class OptionForm(ModelForm):
options = forms.ChoiceField(widget=forms.RadioSelect())
class Meta:
model = Item
fields = ( 'options', )
When i render the form in the template it renders all available options for the Item object(the expected behavior) even those not created by a specific item. I want to be able to load options defined by the specific Item that will be chosen by the user. How do i override the form to achieve such behavior.for example without a form i can render an Items own Options through its id. item = Item.objects.get(pk=id)
Its tough to make ModelForm's defined on the fly because they are intimately tied to the structure in your model. Nevertheless you could use some clever template control flow and view rendering to get your desired effect. This is untested so you millage with this might vary.
<form method="post" action="">
{{ formset.management_form }}
{% for form in formset %}
{{ form.id }}
<ul>
{% if user_option.category %}
<li>{{ form.caregory }}</li>
{% endif %}
{% if user_option.name %}
<li>{{ form.name }}</li>
{% endif %}
{% if user_option.p_opt %}
<li>{{ form.price }}</li>
<li>{{ form.options }}</li>
{% endif %}
</ul>
{% endfor %}
</form>
From the Djano docs here.
Try overriding the form's init method and passing in the Item pk as an additional argument. The trick here is to pop the argument before calling the parent init.
class ItemOptionsForm(forms.ModelForm):
class Meta:
model = Item
def __init__(self, *args, **kwargs):
# pop 'item_id' as parent's init is not expecting it
item_id = kwargs.pop('item_id', None)
# now it's safe to call the parent init
super(ItemOptionsForm, self).__init__(*args, **kwargs)
# Limit options to only the item's options
if item_id:
try:
item = Item.objects.get(id=item_id)
except:
raise ValidationError('No item found!')
self.fields['options'] = forms.ChoiceField(item.options)
Then, in your view, create the form like:
form = ItemOptionsForm(item_id=item_id)
The nice thing about this is that you can raise ValidationErrors that will show up in the form.
Be aware that this doesn't prevent someone from POSTing option IDs to your form which do not belong to the Item, so you'll likely want to override the ModelForm.clean() to validate the options as well.
learning from link django: How to limit field choices in formset? provided by #jingo, i solved the problem by first of all creating dynamic form like so;
def partial_order_item_form(item):
"""dynamic form limiting optional_items to their items"""
class PartialOrderItemform(forms.Form):
quantity = forms.IntegerField(widget=forms.TextInput(attrs={'size':'2', 'class':'quantity','maxlength':'5'}))
option = forms.ModelChoiceField(queryset=OptionalItems.objects.filter(item=item),widget= forms.RadioSelect())
return PartialOrderItemform
then validating form like so;
def show_item(request,id):
option = get_object_or_404(Item,pk=id)
if request.method == 'POST':
form = partial_order_item_form(option)
#bound form to POST data,
final_form = form(request.POST)
# check validation of posted data
if final_form.is_valid():
order.add_to_order(request)
url =urlresolvers.reverse('order_index',kwargs={'id':a.id})
# redirect to order page
return HttpResponseRedirect(url)
else:
form = partial_order_item_form(item=id)
context={
'form':form,
}
return render_to_response('item.html',context,context_instance=RequestContext(request))