Pass current product_id to a Model in Django
I'm trying to give django a try by developing a simple page where people can ask something about a product
This is my model, i can create products in the admin area, display the product page, and the form shows up with the fields email, and text.
class Product(models.Model):
category = models.ForeignKey(Category)
title = models.CharField(max_length=100)
text = models.TextField()
class Question(models.Model):
email = models.CharField(max_length=100)
product = models.ForeignKey(Product, default=?, editable=False)
date = models.DateTimeField(auto_now=True, editable=False)
text = models.TextField()
class QuestionForm(ModelForm):
class Meta:
model = Question
But i don't know how to tell the model which product id the question has to be saved to.
This is my views.py
# other stuff here
def detail(request, product_id):
p = get_object_or_404(Product, pk=product_id)
f = QuestionForm()
return render_to_response('products/detail.html', {'title' : p.title, 'productt': p, 'form' : f},
context_instance = RequestContext(request))
def question(request, product_id):
p = get_object_or_404(Product, pk=product_id)
f = QuestionForm(request.POST)
new_question = f.save()
return HttpResponseRedirect(reverse('products.views.detail', args=(p.id,)))
And the URL
urlpatterns = patter开发者_运维问答ns('products.views',
(r'^products/$', 'index'),
(r'^products/(?P<product_id>\d+)/$', 'detail'),
(r'^products/(?P<product_id>\d+)/question/$', 'question')
)
Righ now it works if i put a "1" in the default attribute for the product foreign key in the question model (where the question mark is), it saves the question to the product id 1. But i don't know what to do to make it save to the current product.
You can either:
Send product_id
as form value
Make product
foreign key in your form a hidden field and set it's value to primary key value of the product in detail
view. This way you don't need a product_id
in your question
view URL and arguments since the ID will be passed with POST
data. (see link for examples)
Id would use this option, since you'd have cleaner question
URL and you could do more validation in your form on the product.
or
Send product_id
through URL
Use reverse in your detail
view to build the form action
attribute or use url
template tag to build the action
attribute in form template. This way you need product_id
in your question
URL and arguments but you don't need product
field in your QuestionForm
. Then in question
view simply get the product instance and set it as FK value on Question
.
Example:
Using url
template tag in products/detail.html
:
<form action="{% url question product.pk %}" method="post">
....
</form>
Or use reverse
in detail
view:
def detail(request, product_id):
p = get_object_or_404(Product, pk=product_id)
f = QuestionForm()
return render_to_response('products/detail.html', {
'title' : p.title,
'product': p,
'action': reverse("question", args=[p.pk,]),
'form' : f},
context_instance = RequestContext(request))
your template:
<form action="{{ action }}" method="post">
....
</form>
Either way your question
view would need a line added which actually sets product instance to Question
attribute:
def question(request, product_id):
...
new_question.product = p
精彩评论