我正在使用Django 2.0
我有一张Note
桌子和StarredNotes
一张桌子。
最初,没有id
字段,因为它默认添加Django
为integer
数据类型。
现在我已将数据类型更改为id
模型UUID
中
模型.py
class Starred(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
user = models.ForeignKey(User, on_delete=models.CASCADE)
note = models.ForeignKey(Note, on_delete=models.CASCADE)
objects = StarredManager()
class ColorLabels(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
title = models.CharField(max_length=100, unique=True)
value = models.CharField(max_length=100)
default = models.BooleanField(default=False)
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title + '(' + self.value + ')'
和views.py
class StarredNotes(ListView):
template_name = 'notes/starred.html'
model = Starred
context_object_name = 'starred_notes'
def get_queryset(self):
starred_notes = Starred.objects.filter(user=self.request.user).order_by('-updated')
return starred_notes
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
return super(self.__class__, self).dispatch(request, *args, **kwargs)
和urls.py
app_name = 'notes'
urlpatterns = [
url(r'^$', MyNotes.as_view()),
url(r'^my/$', MyNotes.as_view(), name='my'),
path('<pk>/', NoteUpdate.as_view(), name='update'),
path('share-public/<pk>/', ShareNotePublic.as_view(), name='share-public'),
path('starred-notes/', StarredNotes.as_view(), name='starred'),
path('shared-notes/', SharedNotes.as_view(), name='shared'),
path('delete/<pk>/', DeleteNote.as_view(), name='delete'),
path('new/', NewNote.as_view(), name='new'),
path('<pk>/public/', PublicNoteView.as_view(), name='public-view'),
]
但是当我使用访问视图时
http://127.0.0.1:1234/notes/starred-notes/
它给出了错误
ValidationError at /notes/starred-notes/
["'starred-notes' is not a valid UUID."]