【问题标题】:Pass current product_id to a Model in Django将当前 product_id 传递给 Django 中的模型
【发布时间】:2011-05-28 22:29:17
【问题描述】:

我正在尝试通过开发一个简单的页面来尝试 django,人们可以在其中询问有关产品的信息

这是我的模型,我可以在管理区域创建产品,显示产品页面,然后表单会显示字段电子邮件和文本。

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

但我不知道如何告诉模型问题必须保存到哪个产品 ID。

这是我的观点.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,)))

还有网址

urlpatterns = patterns('products.views',
    (r'^products/$', 'index'),
    (r'^products/(?P<product_id>\d+)/$', 'detail'),
    (r'^products/(?P<product_id>\d+)/question/$', 'question')
)

现在,如果我在问题模型(问号所在的位置)的产品外键的默认属性中输入“1”,它可以工作,它将问题保存到产品 id 1。但我没有知道如何将其保存到当前产品中。

【问题讨论】:

    标签: python django


    【解决方案1】:

    您可以:

    发送product_id作为表单值

    创建product foreign key in your form a hidden field 并将其值设置为detail 视图中产品的主键值。这样,您不需要question 视图 URL 和参数中需要 product_id,因为 ID 将与 POST 数据一起传递。 (示例见link

    Id 将使用此选项,因为您将拥有更简洁的 question URL,并且您可以在产品的表单中进行更多验证。

    通过网址发送product_id

    detail 视图中使用reverse 构建表单action 属性或使用url template tag 在表单模板中构建action 属性。这样,您的question URL 和参数中需要product_id,但您的QuestionForm 中不需要product 字段。然后在question 视图中只需获取产品实例并将其设置为Question 上的FK 值。


    示例:

    products/detail.html中使用url模板标签:

    <form action="{% url question product.pk %}" method="post">
     .... 
    </form>
    

    或在detail 视图中使用reverse

    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))
    

    你的模板:

    <form action="{{ action }}" method="post">
     .... 
    </form>
    

    无论哪种方式,您的 question 视图都需要添加一行,该行实际上将产品实例设置为 Question 属性:

    def question(request, product_id):
        ...
        new_question.product = p
    

    【讨论】:

    • 我的 url 中已经有 product_id,所以现在我把它写成这样:new_question = f.save(commit=False) 然后这一行 new_question.post = p 然后 new_question.save() 它就像一个魅力!谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-03
    • 2016-11-17
    • 1970-01-01
    • 2011-08-21
    相关资源
    最近更新 更多