models.OneToOneField(Category,default='category') This is not set in form and not set in view and is not null so when you do post.save() error was raised and post is not saved.
I don't sure that you can pass str as default for OnetoOne and I am not sure that you need a OnetoOne field I think foreignkey us better for you, because have not sense have one category for every post, I think you want several post in one category. El martes, 28 de junio de 2016, kaustubh tripathi <[email protected]> escribió: > Hi everyone! > > I am creating a simple webapp using django where a user can login, choose one of the given category and create a post under the chosen category. > > I am having trouble in creating a post. When I create a new post through django form the date isn't saved to database. I log in to the django-admin to check if the post is created but the 'post' table remains empty. > > Here are the necessary code snippets: > > "Category model" > class Category(models.Model): > > name = models.CharField(max_length=128,unique=True) > slug = models.SlugField() > def save(self,*args,**kwargs): > self.slug = slugify(self.name) > super(Category,self).save(*args,**kwargs) > > def __unicode__(self): > return self.name > > "Post Model" > class Post(models.Model): > category = models.OneToOneField(Category,default='category') > title = models.CharField(max_length=128,null=True) > content = models.TextField(blank=True,null=True) > > def __unicode__(self): > return self.title > > "Postform/forms.py" > > class PostForm(forms.ModelForm): > title = forms.CharField(max_length=128) > content = forms.CharField(widget=forms.Textarea) > class Meta: > model = Post > fields = ('title','content') > > "create_post function/views.py" > > def create_post(request,category_name_slug): > > created = False > instance = get_object_or_404(Category,slug=category_name_slug) > if request.method == 'POST': > form = PostForm(data=request.POST or None,instance=instance) > if form.is_valid(): > post = form.save(commit=False) > post.save() > created = True > else: > print form.errors > else: > form = PostForm() > context={ > 'form':form, > 'instance':instance, > 'created':created > } > return render(request,"add_post.html",context) > > > Any solution?? > > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > 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/cdf3882e-636e-4d2b-b626-6b60aaea9f5c%40googlegroups.com . > For more options, visit https://groups.google.com/d/optout. > -- "La utopía sirve para caminar" Fernando Birri -- 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 [email protected]. To post to this group, send email to [email protected]. 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/CAG%2B5VyNpkULru70Wk3vOa2Sqkwd1HyX9Xy8X-Yt%3Du8gdDPZVTg%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.

