(My English is not so good so I'll just try to be simple, sorry guys) So I have a model TopList which has a slug field so I can look up its object by slug.
class TopList(models.Model): name = models.CharField(max_length=200) slug = models.SlugField(max_length=200, default='') def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.set_slug() def save(self, *args, **kwargs): super().save(*args, **kwargs) self.set_slug() def set_slug(self): self.slug = slugify(self.name, allow_unicode=True) The problem is when I use a ModelForm to create object, the slug field is set correctly, but when I use it to look up the object, it returns "lists.models.DoesNotExist: TopList matching query does not exist" (I tried it in the shell): # forms.py class TopListForm(ModelForm): class Meta: model = TopList fields = ('name',) # Item is just a model which has foreignkey relationship with TopList ItemFormSet = inlineformset_factory(TopList, Item, fields=('name',)) # views.py def new_list(request): form = TopListForm() item_formset = ItemFormSet() if request.method == 'POST': form = TopListForm(request.POST) if form.is_valid(): created_list = form.save(commit=False) item_formset = ItemFormSet(request.POST, instance=created_list) if item_formset.is_valid(): created_list.save() item_formset.save() return redirect('/danh-sach') return render(request, 'lists/new_list.html', context={'form': form, 'item_formset': item_formset}) But if I use TopList.objects.create(name='something') to create object, slug is still set correctly, and I can retrieve it by slug. # "test test" is an object I created earlier using form >>> TopList.object.get(slug='test-test') Traceback (most recent call last): File "<console>", line 1, in <module> AttributeError: type object 'TopList' has no attribute 'object' >>> TopList.objects.get(slug='test-test') Traceback (most recent call last): File "<console>", line 1, in <module> File "/home/vietphuong/Documents/Python/toptenproject/venv/lib/python3.5/site-packages/django/db/models/manager.py" , line 85, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "/home/vietphuong/Documents/Python/toptenproject/venv/lib/python3.5/site-packages/django/db/models/query.py" , line 385, in get self.model._meta.object_name lists.models.DoesNotExist: TopList matching query does not exist. >>> TopList.objects.create(name='another test') <TopList: another test> >>> TopList.objects.get(slug='another-test') <TopList: another test> Another thing is after I try to save that "test test" object, just save no changing at all, I can now look it up. >>> TopList.objects.get(name='test test') <TopList: test test> >>> t = TopList.objects.get(name='test test') >>> t.slug 'test-test' >>> t.save() >>> TopList.objects.get(slug='test-test') <TopList: test test> -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d72c1256-5b10-4d49-b0a1-a4bed16151db%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.