我创建了两个模型w_orders(workorders)和tobjects。我有两个模板,一个仅用于创建工作单,并且可以正常工作,另一个模板包含工作单和对象的数据。 tobjects与表w_orders相关,我希望能够为一个工作订单创建x个对象。对于每个工作单,将自动创建一个对象。到目前为止,我已经创建了模板,但是我还不太清楚如何仅显示特定工作订单的相关对象。例如,对于第1个工作单,我得到了为其他工作单创建的对象。同样,我无法添加或编辑与工作单相关的对象。这是我的代码:
models.py
class w_orders(models.Model):
Id = models.BigAutoField(primary_key=True)
datedefwo = models.DateField(default=datetime.now)
datesched = models.DateField(blank=True, null=True)
datefinished = models.DateField(blank=True, null=True)
sign = models.BigIntegerField(blank=True, null=True)
statusid = models.BigIntegerField(blank=True, null=True, default=1, choices=STATUS_CHOICES)
typeid = models.BigIntegerField(blank=True, null=True, default=1, choices=TYPE_CHOICES)
comments = models.CharField(max_length=254, blank=True, null=True)
navid = models.BigIntegerField(blank=True, null=True)
navkonsid = models.CharField(max_length=12, blank=True, null=True)
navname = models.CharField(max_length=254, blank=True, null=True)
navcustadr = models.CharField(max_length=254, blank=True, null=True)
navdebt = models.FloatField(blank=True, null=True)
navpropcode = models.CharField(max_length=254, blank=True, null=True)
navdepcode = models.CharField(max_length=254, blank=True, null=True)
navphoneno = models.CharField(max_length=254, blank=True, null=True)
navreasoncomp = models.CharField(max_length=254, blank=True, null=True)
nightshift = models.BooleanField(default=False)
priority = models.BigIntegerField(blank=True, null=True)
stid = models.BigIntegerField(blank=True, null=True)
mapurl = models.CharField(max_length=254, blank=True, null=True)
def __unicode__(self):
return self.Id
class tobjects(models.Model):
oid = models.BigAutoField(primary_key=True)
wid = models.ForeignKey(w_orders, on_delete=models.CASCADE)
objtypegisid = models.BigIntegerField(blank=True, null=True, default=1)
objgisid = models.BigIntegerField(blank=True, null=True, default=1)
condgenid = models.BigIntegerField(blank=True, null=True, default=1)
condriskid = models.BigIntegerField(blank=True, null=True, default=1)
condratsid = models.BigIntegerField(blank=True, null=True, default=1)
condmhcoverid = models.BigIntegerField(blank=True, null=True, default=1)
condmhwallid = models.BigIntegerField(blank=True, null=True, default=1)
condpipehydrsid = models.BigIntegerField(blank=True, null=True, default=1)
condpipehydreid = models.BigIntegerField(blank=True, null=True, default=1)
condpipedepvolsid = models.BigIntegerField(blank=True, null=True, default=1)
condpipedepvoleid = models.BigIntegerField(blank=True, null=True, default=1)
condpipedepconssid = models.BigIntegerField(blank=True, null=True, default=1)
condpipedepconseid = models.BigIntegerField(blank=True, null=True, default=1)
txt = models.CharField(max_length=254, blank=True, null=True)
def __unicode__(self):
return self.oid
@receiver(post_save, sender=w_orders)
def ensure_order_exists(sender, **kwargs):
if kwargs.get('created', False):
tobjects.objects.get_or_create(wid=kwargs.get('instance'))
workorder_edit.html
{% extends 'workorders/base.html' %}
{% block jumbotron2 %}
<div class="jumbotron">
<h1>Navbar example</h1>
<p class="lead">This example is a quick exercise to illustrate how the top-aligned navbar works. As you scroll, this navbar remains in its original position and moves with the rest of the page.</p>
<a class="btn btn-lg btn-primary" href="../../components/navbar/" role="button">View navbar docs »</a>
</div>
{% endblock %}
{% load static %}
{% block content %}
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
{{ formset.management_form }}
{% for form in formset %}
{{ form.as_p }}
{% endfor %}
<input type="submit" value="Save">
</form>
workorder_create.html
{% extends 'workorders/base.html' %}
{% block jumbotron2 %}
<div class="jumbotron">
<h1>Navbar example</h1>
<p class="lead">This example is a quick exercise to illustrate how the top-aligned navbar works. As you scroll, this navbar remains in its original position and moves with the rest of the page.</p>
<a class="btn btn-lg btn-primary" href="../../components/navbar/" role="button">View navbar docs »</a>
</div>
{% endblock %}
{% load static %}
{% block content %}
<form method="POST" action="">
{% csrf_token %}
<div class="page-header">
<h2> Create Workorder </h2>
</div>
{{ form.non_field_errors }}
<div class="row">
<div class="col-md-4">
{{ form.datedefwo.errors }}
<label for="{{ form.datedefwo.id_for_label }}">Defined:</label>
{{ form.datedefwo }}
</div>
</div>
<br>
<div class="row">
<div class="col-md-4">
{{ form.typeid.errors }}
<label for="{{ form.typeid.id_for_label }}">Order type:</label>
{{ form.typeid }}
</div>
</div>
<br>
<div class="row">
<div class="col-md-4">
{{ form.datesched.errors }}
<label for="{{ form.datesched.id_for_label }}">Date Scheduled:</label>
{{ form.datesched }}
</div>
</div>
<br>
<div class="row">
<div class="col-md-4">
{{ form.statusid.errors }}
<label for="{{ form.statusid.id_for_label }}">Status:</label>
{{ form.statusid }}
</div>
</div>
<br>
<div class="row">
<div class="col-md-4">
{{ form.navreasoncomp.errors }}
<label for="{{ form.navreasoncomp.id_for_label }}">Request:</label>
{{ form.navreasoncomp }}
</div>
</div>
<div class="row">
<div class="col-md-4">
{{ form.navcustadr.errors }}
<label for="{{ form.navcustadr.id_for_label }}">Address:</label>
{{ form.navcustadr }}
</div>
</div>
<div class="row">
<div class="col-md-4">
{{ form.navname.errors }}
<label for="{{ form.navname.id_for_label }}">Customer:</label>
{{ form.navname }}
</div>
</div>
<div class="row">
<div class="col-md-4">
{{ form.navphoneno.errors }}
<label for="{{ form.navphoneno.id_for_label }}">Tel No:</label>
{{ form.navphoneno }}
</div>
</div>
<div class="row">
<div class="col-md-4">
{{ form.mapurl.errors }}
<label for="{{ form.mapurl.id_for_label }}">Map:</label>
{{ form.mapurl }}
</div>
</div>
<br>
<input type="submit" value="Save">
</form>
{% endblock %}
views.py
def workorders_list(request):
workorders_all = w_orders.objects.all().values_list('Id', flat=True)
return render(request, "workorders_list.html")
def home_page(request):
return render(request, "home_page.html")
def workorder_create(request):
if request.method == "POST":
form = CreateWorkorder(request.POST)
if form.is_valid():
CreateWorkorder_item = form.save(commit=False)
CreateWorkorder_item.save()
return redirect('/workorder/' + str(CreateWorkorder_item.Id) + '/')
else:
form = CreateWorkorder()
return render(request, 'workorders/workorder_create.html', {'form': form})
tobjectsFormSet = modelformset_factory(tobjects, fields=['wid', 'objtypegisid', 'objgisid', 'condgenid', 'condriskid', 'condratsid', 'condmhcoverid', 'condmhwallid', 'condpipehydrsid', 'condpipehydreid', 'condpipedepvolsid', 'condpipedepvoleid', 'condpipedepconssid', 'condpipedepconseid', 'txt'])
def workorder_edit(request, id=None):
item = get_object_or_404(w_orders, Id=id)
form = CreateWorkorder(request.POST or None, instance=item)
if form.is_valid():
new_order=form.save()
new_order.save()
formset = tobjectsFormSet(request.POST or None, request.FILES or None, extra=0)
if formset.is_valid():
formset.save()
return redirect('/workorder/' + str(item.Id) + '/')
else:
formset = tobjectsFormSet(request.POST or None, request.FILES or None)
return render(request, 'workorders/workorder_edit.html', {'formset': formset, 'form': form})
forms.py
class DateInput(forms.DateInput):
input_type = 'date'
class CreateWorkorder(forms.ModelForm):
navreasoncomp = forms.CharField(widget=forms.TextInput(
attrs={
'class': 'form-control',
}
))
navcustadr = forms.CharField(widget=forms.TextInput(
attrs={
'class': 'form-control',
}
))
navname = forms.CharField(widget=forms.TextInput(
attrs={
'class': 'form-control',
}
))
navphoneno = forms.CharField(widget=forms.TextInput(
attrs={
'class': 'form-control',
}
))
mapurl = forms.CharField(widget=forms.TextInput(
attrs={
'class': 'form-control',
}
))
class Meta:
model = models.w_orders
fields = ['Id', 'datedefwo', 'datesched', 'datefinished', 'statusid', 'sign', 'typeid', 'comments', 'navid', 'navkonsid', 'navdebt', 'navpropcode', 'navdepcode', 'nightshift', 'priority', 'stid', 'datesched', 'statusid', 'navreasoncomp', 'navcustadr', 'navname', 'navphoneno', 'mapurl']
widgets = {
'datedefwo': DateInput(attrs={'class':'datepicker'}),
'datesched': DateInput(attrs={'class':'datepicker'}),
'datefinished': DateInput(attrs={'class':'datepicker'}),
'navreasoncomp': forms.Textarea(attrs={'rows':3, 'cols':42}),
}
class createobject(forms.ModelForm):
wid = forms.IntegerField(widget=forms.TextInput(
attrs={
'class': 'form-control',
}
))
txt = forms.CharField(widget=forms.TextInput(
attrs={
'class': 'form-control',
}
))
class Meta:
model = models.tobjects
fields = ['wid', 'objtypegisid', 'objgisid', 'condgenid', 'condriskid', 'condratsid', 'condmhcoverid', 'condmhwallid', 'condpipehydrsid', 'condpipehydreid', 'condpipedepvolsid', 'condpipedepvoleid', 'condpipedepconssid', 'condpipedepconseid', 'txt']
urls.py
urlpatterns = [
url(r'^workorder/create/$', views.workorder_create, name='create_workorder'),
url(r'^workorder/edit/(?P<id>\d+)/$', views.workorder_edit, name='edit_workorder'),
url(r'^workorder/(?P<id>\d+)/$', views.workorder, name='workorder'),
url(r'^home/$', views.home_page, name='home_page'),
url(r'^workorders/$', views.workorders_list, name='workorders_list'),