Closed bogdal closed 9 years ago
Thanks for the catch. Changed example 0.7
to define the slugfield thus: slug = models.SlugField(unique=True)
.
:ship:
@pydanny unique=True
is not good enough in our case, we should rather use primary_key=True
.
Generally I see here two options:
id
field as a primary key# models.py
slug = models.SlugField(unique=True)
# views.py
flavor = get_object_or_404(Flavor, slug=kwargs['slug'])
slug
field as a primary key# models.py
slug = models.SlugField(primary_key=True)
# views.py
flavor = get_object_or_404(Flavor, pk=kwargs['slug'])
Right now we mix both options, therefore current solution still doesn't work.
unique=True
adds an index. Which makes it fine to use slug
as the primary identifier.
Reference: https://docs.djangoproject.com/en/1.8/ref/models/fields/#unique
I agree with you, but in the following line we should use slug
instead of pk
:
flavor = get_object_or_404(Flavor, pk=kwargs['slug'])
Currently this line says: give me the flavor which id
(default primary key field) is equal kwargs['slug']
, what is incorrect.
Ha ha ha!. Thank you so much for having a good eye. :smile_cat:
:ship:
Hi there,
In examples
10.13
and10.14
we have the following line:But
slug
field in the definition of modelFlavor
(example10.7
) is not a primary key, hence instead ofpk
in the above line we should useslug
: