在childs对象中设置父对象值';是Django的模型形式吗?

在childs对象中设置父对象值';是Django的模型形式吗?,django,python-3.x,django-forms,Django,Python 3.x,Django Forms,我希望在孩子的模型中预先填充一个家长变量,特别是序列号。我已经设法将序列号作为URL的一部分,但我想知道如何将其作为表单页面上的变量实现 型号.py class Product(models.Model): serial_number = models.CharField(unique=True, max_length=15) class ProductInstance(models.Model): serial_number = models.ForeignKey('Product'

我希望在孩子的模型中预先填充一个家长变量,特别是序列号。我已经设法将序列号作为URL的一部分,但我想知道如何将其作为表单页面上的变量实现

型号.py

class Product(models.Model):
  serial_number = models.CharField(unique=True, max_length=15)

class ProductInstance(models.Model):
  serial_number = models.ForeignKey('Product', on_delete=models.SET_NULL, null=True)
class ProductInstanceCreate(CreateView):
  model = ProductInstance
  template_name = 'myapp/edit_productinstance.html'
  form_class = GunInstanceForm

def get_success_url(self):
   return reverse_lazy ('product-detail', kwargs={'pk': self.object.serial_number.pk})
class ProductInstanceForm(forms.ModelForm):

class Meta:
    model = ProductInstance
    fields = '__all__'
urlpatterns += [  
url(r'^productinstance/(?P<serial_number>[-\w]+)/create/$', views.ProductInstanceCreate.as_view(), name='productinstance_create'),]
视图.py

class Product(models.Model):
  serial_number = models.CharField(unique=True, max_length=15)

class ProductInstance(models.Model):
  serial_number = models.ForeignKey('Product', on_delete=models.SET_NULL, null=True)
class ProductInstanceCreate(CreateView):
  model = ProductInstance
  template_name = 'myapp/edit_productinstance.html'
  form_class = GunInstanceForm

def get_success_url(self):
   return reverse_lazy ('product-detail', kwargs={'pk': self.object.serial_number.pk})
class ProductInstanceForm(forms.ModelForm):

class Meta:
    model = ProductInstance
    fields = '__all__'
urlpatterns += [  
url(r'^productinstance/(?P<serial_number>[-\w]+)/create/$', views.ProductInstanceCreate.as_view(), name='productinstance_create'),]
Forms.py

class Product(models.Model):
  serial_number = models.CharField(unique=True, max_length=15)

class ProductInstance(models.Model):
  serial_number = models.ForeignKey('Product', on_delete=models.SET_NULL, null=True)
class ProductInstanceCreate(CreateView):
  model = ProductInstance
  template_name = 'myapp/edit_productinstance.html'
  form_class = GunInstanceForm

def get_success_url(self):
   return reverse_lazy ('product-detail', kwargs={'pk': self.object.serial_number.pk})
class ProductInstanceForm(forms.ModelForm):

class Meta:
    model = ProductInstance
    fields = '__all__'
urlpatterns += [  
url(r'^productinstance/(?P<serial_number>[-\w]+)/create/$', views.ProductInstanceCreate.as_view(), name='productinstance_create'),]
模板/myapp/product\u detail.html

...
<a href="{% url 'productinstance_create' serial_number=product.serial_number %}">New</a>
...
{% extends "base_generic.html" %}
{% block content %}


<h2>Serial Number: {{ serial_number }}</h2>
</br>
<form action="" method="post">

    {% csrf_token %}
    <table>
    {{ form }}
    </table>
    <input type="submit" value="Submit" />
</form>
</br>
<a href="">Back</a>

{% endblock %} 
。。。
...
url.py

class Product(models.Model):
  serial_number = models.CharField(unique=True, max_length=15)

class ProductInstance(models.Model):
  serial_number = models.ForeignKey('Product', on_delete=models.SET_NULL, null=True)
class ProductInstanceCreate(CreateView):
  model = ProductInstance
  template_name = 'myapp/edit_productinstance.html'
  form_class = GunInstanceForm

def get_success_url(self):
   return reverse_lazy ('product-detail', kwargs={'pk': self.object.serial_number.pk})
class ProductInstanceForm(forms.ModelForm):

class Meta:
    model = ProductInstance
    fields = '__all__'
urlpatterns += [  
url(r'^productinstance/(?P<serial_number>[-\w]+)/create/$', views.ProductInstanceCreate.as_view(), name='productinstance_create'),]
urlpatterns+=[
url(r'^productinstance/(?P[-\w]+)/create/$,views.ProductInstanceCreate.as_view(),name='productinstance_create'),]
模板/myapp/edit\u productinstance\u form.html

...
<a href="{% url 'productinstance_create' serial_number=product.serial_number %}">New</a>
...
{% extends "base_generic.html" %}
{% block content %}


<h2>Serial Number: {{ serial_number }}</h2>
</br>
<form action="" method="post">

    {% csrf_token %}
    <table>
    {{ form }}
    </table>
    <input type="submit" value="Submit" />
</form>
</br>
<a href="">Back</a>

{% endblock %} 
{%extensed“base_generic.html”%}
{%block content%}
序列号:{{序列号}

{%csrf_令牌%} {{form}}
{%endblock%}
因此,目前我可以创建一个URL,例如:productinstance/D1430913/create/

我现在需要知道:

  • 如何将其用作标题的变量

  • 如何将表单默认值设置为它

  • 标题:

    <h2>Serial Number: {{ form.serial_number.value }}</h2>