Hello,

you have an error here:

|    defsave(self,*args,**kwargs):
        super().save(*args,**kwargs)
        self.set_slug()
|

self.set_slug() should be before super().save().

Regards,

Antonis Christofides
http://djangodeployment.com

On 2016-12-10 02:26, Viet Phuong wrote:
> (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.
>
> |
> classTopList(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()
>
>     defsave(self,*args,**kwargs):
>         super().save(*args,**kwargs)
>         self.set_slug()
>
>     defset_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
> classTopListForm(ModelForm):
>     classMeta:
>         model =TopList
>         fields =('name',)
>
> # Item is just a model which has foreignkey relationship with TopList
> ItemFormSet=inlineformset_factory(TopList,Item,fields=('name',))
> |
>
> |
> # views.py
> defnew_list(request):
>     form =TopListForm()
>     item_formset =ItemFormSet()
>
>     ifrequest.method =='POST':
>         form =TopListForm(request.POST)
>         ifform.is_valid():
>             created_list =form.save(commit=False)
>             item_formset =ItemFormSet(request.POST,instance=created_list)
>             ifitem_formset.is_valid():
>                 created_list.save()
>                 item_formset.save()
>                 returnredirect('/danh-sach')
>
>    
> returnrender(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 noattribute '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,inmanager_method
>     returngetattr(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,inget
>     self.model._meta.object_name
> lists.models.DoesNotExist:TopListmatching query does notexist.
> >>>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
> <mailto:django-users+unsubscr...@googlegroups.com>.
> To post to this group, send email to django-users@googlegroups.com
> <mailto: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
> <https://groups.google.com/d/msgid/django-users/d72c1256-5b10-4d49-b0a1-a4bed16151db%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

-- 
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/a542da67-f0c6-18b5-498c-4729d5adafd9%40djangodeployment.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to