简体   繁体   English

Django将对象传递给另一种形式的模型

[英]Django pass object to model of another form

I have implemented a Python form/view/template that lets you configure different notifications for a product:我已经实现了一个 Python 表单/视图/模板,可让您为产品配置不同的通知:

models.py模型.py

class PNotification(models.Model):
    add = models.BooleanField(default=False, help_text='Receive messages when an engagement is 
    added')
    delete = models.BooleanField(default=False, help_text='Receive 


class Product(models.Model):

forms.py表格.py

class PNotificationForm(forms.ModelForm):
    class Meta:
        model = PNotification
        exclude = ['']


class ProductForm(forms.ModelForm):

The PNotification has an own view and template: PNotification 有自己的视图和模板:

<h3> Edit Notifications for {{ product.name }}</h3>
    <form class="form-horizontal" method="post">{% csrf_token %}
        {% include "dojo/form_fields.html" with form=form %}
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <input class="btn btn-primary" type="submit" value="Submit"/>
            </div>
        </div>
    </form>

When I call the page (edit/product_id/notifications) to edit an products notifications, the h3 is set correct, so the product is passed to the form/template.当我调用页面 (edit/product_id/notifications) 来编辑产品通知时, h3设置正确,因此产品被传递到表单/模板。

My problem is: I need to link PNotification with a product.我的问题是:我需要将 PNotification 与产品相关联。

How can I pass product.id to my PNotificationForm so I can save it there?如何将product.id传递给我的PNotificationForm以便我可以将其保存在那里? This field should be the primary key of PNotification.该字段应该是 PNotification 的主键。

Edit编辑

Here is my view.py:这是我的view.py:

def edit_product_notifications(request, pid):
    prod = Product.objects.get(pk=pid)
    logger.info('Editing product')
    if request.method == 'POST':
        pnotification = PNotificationForm(request.POST, instance=prod)
        if pnotification.is_valid():
            pnotification.save()
            logger.info('saved')
        
    else:
        pnotification = PNotificationForm()
        if pnotification.is_valid():
            pnotification.save()
            logger.info('saved')

logger.info(PNotification.objects.get(id=1).msteams)
logger.info('returning')
return render(request,
              'dojo/edit_product_notifications.html',
              {'form': pnotification,
               'product': prod
               }). 

I am passing the product id and then getting it from objects.get() .我正在传递产品 ID,然后从objects.get()获取它。

In urls.py the path to that view needs to have <int:pk> .urls.py中,该视图的路径需要具有<int:pk> The path would look like this edit/<int:pn_pk>/notifications路径看起来像这样edit/<int:pn_pk>/notifications

In your view you need to get the kwargs which is the argument passed by the url.在您看来,您需要获取kwargs ,它是 url 传递的参数。

In a function based view:在基于函数的视图中:

def your_view_name(request, pk):
    # use pk how you want.
    PN = PNotification.objects.get(pk=pn_pk)
    return render(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM