【发布时间】:2020-01-24 04:10:13
【问题描述】:
初始错误:当 CustomerID 在 models.py 中为 CharField 时:
在对模型进行更改并使 CustomerID UUIDField 出现以下错误后:
我正在尝试根据 CustomerID 重定向到视图,但它不适用于 UUID。 以前它可以很好地处理整数。
请提出建议。
url.py:
from django.urls import path, include
from . import views
urlpatterns = [
path('create/', views.create, name='create'),
path('<uuid:customer_id>', views.detail, name='detail'),
path('search/customers/<uuid:customer_id>', views.detail, name='detail'),
path('customers/<uuid:customer_id>', views.detail, name='detail'),
path('edit/<uuid:customer_id>', views.edit, name='edit'),
path('modify/<uuid:customer_id>', views.modify, name='modify'),
]
views.py
@login_required
def detail(request, customer_id):
customer = get_object_or_404(CustomerMaster, pk=customer_id)
return render(request, 'customers/detail.html',{'customer':customer})
models.py
class CustomerMaster(models.Model):
customerid = models.UUIDField(db_column='CustomerID', primary_key=True) # Field name made lowercase.
customernumber = models.CharField(db_column='CustomerNumber', max_length=50) # Field name made lowercase.
customername = models.CharField(db_column='CustomerName', max_length=50) # Field name made lowercase.
lastmodifiedutc = models.DateTimeField(db_column='LastModifiedUTC') # Field name made lowercase.
lastmodifiedby = models.CharField(db_column='LastModifiedBy', max_length=50) # Field name made lowercase.
active = models.BooleanField(db_column='Active') # Field name made lowercase.
customershortname = models.CharField(db_column='CustomerShortName', max_length=50, blank=True, null=True) # Field name made lowercase.
class Meta:
managed = False
db_table = 'Customer_Master'
def __str__(self):
return self.CustomerName
【问题讨论】:
-
能否提供相关型号?
-
@wfehr 我已经更新了模型。请检查。
标签: django python-3.x django-models django-views django-urls