在 django 中创建了一个模型,缺少主键
Created a modelform in django, primary key is missing
我在 django 中创建了一个模型表单,但浏览器中缺少主键字段。它在哪里?我怎样才能让它出现。如果您需要的代码比我下面提供的更多,请告诉我。
view.py
import autocomplete_light
import datetime
from django.shortcuts import render, render_to_response, RequestContext
from django.http import HttpResponseRedirect
from django.utils.timezone import utc
from django.contrib.auth.decorators import login_required
from error_tracking.models import Incident
from django.contrib.auth import authenticate
from django.contrib.auth.models import User
from haystack.query import SearchQuerySet
from error_tracking.forms import IncidentForm
@login_required
def search_incidents(request):
# form validation
if request.method == 'POST':
form = autocomplete_light.IncidentForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/')
else:
form = IncidentForm()
# grabbing all OPEN incidents
incident_list = Incident.objects.filter(open = 'True').order_by('-incident_date_time_reported')
return render(request, 'search_incidents.html', {
'incident_list' : incident_list,
'user' : request.user,
'form' : form
})
forms.py
import autocomplete_light
from django.forms import ModelForm
from error_tracking.models import Incident
autocomplete_light.register(Incident, search_fields=['incident_id'])
class IncidentForm(autocomplete_light.ModelForm):
class Meta:
model = Incident
fields = ['incident_id', 'user_id', 'equipment_id', 'incident_category', 'incident_date_time_reported', 'incident_date_time_occurred', 'description']
models.py
from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
class Manufacturer(models.Model):
manufacturer = models.CharField(primary_key=True, max_length=100)
def __str__(self):
return self.manufacturer
class Meta:
verbose_name_plural="Manufacturer"
class Equipment_Category(models.Model):
equipment_category = models.CharField(primary_key=True, max_length=100)
def __str__(self):
return self.equipment_category
class Meta:
verbose_name_plural="Equipment Category"
class Equipment(models.Model):
product_id = models.CharField(primary_key=True,max_length=100)
manufacturer = models.ForeignKey(Manufacturer)
equipment_category = models.ForeignKey(Equipment_Category)
validated= models.BooleanField(default=True)
in_service_date = models.DateTimeField('in service date', default=timezone.now)
def __str__(self):
return self.product_id
class Meta:
verbose_name_plural="Equipment"
class Incident_Category(models.Model):
incident_category = models.CharField(primary_key=True,max_length=100)
description = models.CharField(max_length=100)
def __str__(self):
return self.incident_category
class Meta:
verbose_name_plural="Incident Category"
class Incident(models.Model):
incident_id = models.AutoField(primary_key=True)
incident_date_time_reported = models.DateTimeField('incident reported', default=timezone.now)
user_id = models.ForeignKey(User)
incident_date_time_occurred = models.DateTimeField('incident occurred', default=timezone.now)
clinical_sample = models.BooleanField(default=True)
number_of_clinical_samples = models.IntegerField(default=0)
sample_id_numbers = models.TextField(max_length=1000)
equipment_id = models.ForeignKey(Equipment)
incident_category = models.ForeignKey(Incident_Category)
technician = models.TextField(max_length=1000)
description = models.TextField(max_length=1000)
action_taken = models.TextField(max_length=1000)
open = models.BooleanField(default=True)
def __str__(self):
return self.incident_id
class Meta:
verbose_name_plural="Incident"
search_incidents.html
{% extends "base.html" %}
{% block content %}
<br>
<p>Not only have you successfully logged in, you have also made it to the Search Incidents page using html templates</p>
<br><br>
<!--incident_id-->
<!--CAPA number-->
<!--Date From To-->
<!--Creator-->
<!--Status-->
<!--Category-->
<!--Equipment ID-->
<!--Search Description-->
<!--error_tracking.views.search_usernames-->
<form method='POST' action=''>{% csrf_token %}
{{ form.as_p }}
<input type="submit"/>
</form>
<div id="table-wrapper" style="overflow:auto; height:600px;">
<table class="table table-striped">
<thead>
<tr>
<th class="col-md-1">ID</th>
<th class="col-md-1">Open</th>
<th class="col-md-2">Created At</th>
<th class="col-md-1">Category</th>
<th class="col-md-7">Description</th>
</tr>
</thead>
<tbody>
{% for incident in incident_list %}
<tr>
<td class="col-md-1">{{ incident.incident_id }}</td>
<td class="col-md-1">{{ incident.open }}</td>
<td class="col-md-2">{{ incident.incident_date_time_reported }}</td>
<td class="col-md-1">{{ incident.incident_category }}</td>
<td class="col-md-7">{{ incident.description}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
读完 source code, I do not think you could ever, even accidentally, expose an AutoField
给用户:
def pk_is_not_editable(pk):
return ((not pk.editable) or (pk.auto_created or isinstance(pk, AutoField))
如果您真的想将 PK 公开给您的用户(我不知道您为什么要这样做),您必须在模型和表单上显式公开它作为 IntegerField
或 ModelForm
将为您排除它:
import autocomplete_light
from django.forms import ModelForm, IntegerField
from error_tracking.models import Incident
autocomplete_light.register(Incident, search_fields=['incident_id'])
class IncidentForm(autocomplete_light.ModelForm):
class Meta:
model = Incident
fields = ['incident_id', 'user_id', 'equipment_id', 'incident_category', 'incident_date_time_reported', 'incident_date_time_occurred', 'description']
incident_id = IntegerField()
class Incident(models.Model):
incident_id = models.IntegerField(primary_key=True)
incident_date_time_reported = models.DateTimeField('incident reported', default=timezone.now)
user_id = models.ForeignKey(User)
incident_date_time_occurred = models.DateTimeField('incident occurred', default=timezone.now)
clinical_sample = models.BooleanField(default=True)
number_of_clinical_samples = models.IntegerField(default=0)
sample_id_numbers = models.TextField(max_length=1000)
equipment_id = models.ForeignKey(Equipment)
incident_category = models.ForeignKey(Incident_Category)
technician = models.TextField(max_length=1000)
description = models.TextField(max_length=1000)
action_taken = models.TextField(max_length=1000)
open = models.BooleanField(default=True)
def __str__(self):
return self.incident_id
class Meta:
verbose_name_plural="Incident"
我在 django 中创建了一个模型表单,但浏览器中缺少主键字段。它在哪里?我怎样才能让它出现。如果您需要的代码比我下面提供的更多,请告诉我。
view.py
import autocomplete_light
import datetime
from django.shortcuts import render, render_to_response, RequestContext
from django.http import HttpResponseRedirect
from django.utils.timezone import utc
from django.contrib.auth.decorators import login_required
from error_tracking.models import Incident
from django.contrib.auth import authenticate
from django.contrib.auth.models import User
from haystack.query import SearchQuerySet
from error_tracking.forms import IncidentForm
@login_required
def search_incidents(request):
# form validation
if request.method == 'POST':
form = autocomplete_light.IncidentForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/')
else:
form = IncidentForm()
# grabbing all OPEN incidents
incident_list = Incident.objects.filter(open = 'True').order_by('-incident_date_time_reported')
return render(request, 'search_incidents.html', {
'incident_list' : incident_list,
'user' : request.user,
'form' : form
})
forms.py
import autocomplete_light
from django.forms import ModelForm
from error_tracking.models import Incident
autocomplete_light.register(Incident, search_fields=['incident_id'])
class IncidentForm(autocomplete_light.ModelForm):
class Meta:
model = Incident
fields = ['incident_id', 'user_id', 'equipment_id', 'incident_category', 'incident_date_time_reported', 'incident_date_time_occurred', 'description']
models.py
from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
class Manufacturer(models.Model):
manufacturer = models.CharField(primary_key=True, max_length=100)
def __str__(self):
return self.manufacturer
class Meta:
verbose_name_plural="Manufacturer"
class Equipment_Category(models.Model):
equipment_category = models.CharField(primary_key=True, max_length=100)
def __str__(self):
return self.equipment_category
class Meta:
verbose_name_plural="Equipment Category"
class Equipment(models.Model):
product_id = models.CharField(primary_key=True,max_length=100)
manufacturer = models.ForeignKey(Manufacturer)
equipment_category = models.ForeignKey(Equipment_Category)
validated= models.BooleanField(default=True)
in_service_date = models.DateTimeField('in service date', default=timezone.now)
def __str__(self):
return self.product_id
class Meta:
verbose_name_plural="Equipment"
class Incident_Category(models.Model):
incident_category = models.CharField(primary_key=True,max_length=100)
description = models.CharField(max_length=100)
def __str__(self):
return self.incident_category
class Meta:
verbose_name_plural="Incident Category"
class Incident(models.Model):
incident_id = models.AutoField(primary_key=True)
incident_date_time_reported = models.DateTimeField('incident reported', default=timezone.now)
user_id = models.ForeignKey(User)
incident_date_time_occurred = models.DateTimeField('incident occurred', default=timezone.now)
clinical_sample = models.BooleanField(default=True)
number_of_clinical_samples = models.IntegerField(default=0)
sample_id_numbers = models.TextField(max_length=1000)
equipment_id = models.ForeignKey(Equipment)
incident_category = models.ForeignKey(Incident_Category)
technician = models.TextField(max_length=1000)
description = models.TextField(max_length=1000)
action_taken = models.TextField(max_length=1000)
open = models.BooleanField(default=True)
def __str__(self):
return self.incident_id
class Meta:
verbose_name_plural="Incident"
search_incidents.html
{% extends "base.html" %}
{% block content %}
<br>
<p>Not only have you successfully logged in, you have also made it to the Search Incidents page using html templates</p>
<br><br>
<!--incident_id-->
<!--CAPA number-->
<!--Date From To-->
<!--Creator-->
<!--Status-->
<!--Category-->
<!--Equipment ID-->
<!--Search Description-->
<!--error_tracking.views.search_usernames-->
<form method='POST' action=''>{% csrf_token %}
{{ form.as_p }}
<input type="submit"/>
</form>
<div id="table-wrapper" style="overflow:auto; height:600px;">
<table class="table table-striped">
<thead>
<tr>
<th class="col-md-1">ID</th>
<th class="col-md-1">Open</th>
<th class="col-md-2">Created At</th>
<th class="col-md-1">Category</th>
<th class="col-md-7">Description</th>
</tr>
</thead>
<tbody>
{% for incident in incident_list %}
<tr>
<td class="col-md-1">{{ incident.incident_id }}</td>
<td class="col-md-1">{{ incident.open }}</td>
<td class="col-md-2">{{ incident.incident_date_time_reported }}</td>
<td class="col-md-1">{{ incident.incident_category }}</td>
<td class="col-md-7">{{ incident.description}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
读完 source code, I do not think you could ever, even accidentally, expose an AutoField
给用户:
def pk_is_not_editable(pk): return ((not pk.editable) or (pk.auto_created or isinstance(pk, AutoField))
如果您真的想将 PK 公开给您的用户(我不知道您为什么要这样做),您必须在模型和表单上显式公开它作为 IntegerField
或 ModelForm
将为您排除它:
import autocomplete_light
from django.forms import ModelForm, IntegerField
from error_tracking.models import Incident
autocomplete_light.register(Incident, search_fields=['incident_id'])
class IncidentForm(autocomplete_light.ModelForm):
class Meta:
model = Incident
fields = ['incident_id', 'user_id', 'equipment_id', 'incident_category', 'incident_date_time_reported', 'incident_date_time_occurred', 'description']
incident_id = IntegerField()
class Incident(models.Model):
incident_id = models.IntegerField(primary_key=True)
incident_date_time_reported = models.DateTimeField('incident reported', default=timezone.now)
user_id = models.ForeignKey(User)
incident_date_time_occurred = models.DateTimeField('incident occurred', default=timezone.now)
clinical_sample = models.BooleanField(default=True)
number_of_clinical_samples = models.IntegerField(default=0)
sample_id_numbers = models.TextField(max_length=1000)
equipment_id = models.ForeignKey(Equipment)
incident_category = models.ForeignKey(Incident_Category)
technician = models.TextField(max_length=1000)
description = models.TextField(max_length=1000)
action_taken = models.TextField(max_length=1000)
open = models.BooleanField(default=True)
def __str__(self):
return self.incident_id
class Meta:
verbose_name_plural="Incident"