In the model, the way it is represented in the database can be defined with a __str__
function.
def __str__(self):
return self.name
name
of the object as a string in the database for us to reference it and interact with it.TextField
is used when you do not know the length of the input for a string
models.py
settings.py
settings.py
reverse_lazy
for the success_url on the delete
methodurlpatterns = [
path("", ThingListView.as_view(), name='thing_list'),
path('<int:pk>/', ThingDetailView.as_view(), name='thing_detail'),
path('create/', ThingCreateView.as_view(), name='thing_create'),
path('<int:pk>/update/', ThingUpdateView.as_view(), name='thing_update'),
path('<int:pk>/delete/', ThingDeleteView.as_view(), name='thing_delete'),
]
from django.db import models
from django.contrib.auth import get_user_model
from django.urls import reverse
class Thing(models.Model):
name = models.CharField(max_length=256)
rating = models.IntegerField(default=0)
reviewer = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
image_url = models.URLField(default='https://http.cat/404')
reference_url = models.URLField(default='https://http.cat/404')
description = models.TextField(default='')
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('thing_detail', args=[str(self.id)])
class ThingDeleteView(DeleteView):
template_name = 'thing_delete.html'
model = Thing
success_url = reverse_lazy('thing_detail')
CASCADE
= delete everything associated with the removed itemget_user_model
must be imported from django.contrib.auth
get_user_model()
is a function provided by Django that returns the user model class that is currently active in the project.get_user_model()
in your code, it will automatically return the user model class that is currently in use, whether it’s the default User model or a custom model specified in the AUTH_USER_MODEL setting.ADMIN
view, you’ll need a function that tells the browser where to go after creating a new item
get_absolute_url
function we created above