需要帮助在通用视图中获取 form_valid 的正确实例

Need help getting correct instance for form_valid in a generic view

我不知道如何为通用视图的 form_valid 部分获取正确的实例。 我正在尝试允许用户在他们的项目墙上 post(有点像 Facebook)。我需要 post 与单个项目相关(一个用户可以有多个项目)。实例应该是 pk 还是项目名称?任何示例代码或帮助将不胜感激!我很难理解当您创建一个新的 post 时,它如何知道将自己与哪个项目相关联。

观看次数

class NewPost(CreateView):
    model = ProjectPost
    form_class = ProjectPostForm
    template_name = 'howdidu/new_post.html'

    def form_valid(self, form):
        newpost = form.save(commit=False)
        form.instance.user = self.request.user
        newpost.save()
        self.object = newpost
        return super(NewPost, self).form_valid(form)

    def get_success_url(self):
        project_username = self.request.user.username
        project_slug = self.object.slug
        return reverse('user_project', kwargs={'username':project_username, 'slug': project_slug})

型号

class UserProject(models.Model):
    user = models.ForeignKey(User)
    title = models.CharField(max_length=100)
    project_overview = models.CharField(max_length=1000)
    project_picture = models.ImageField(upload_to='project_images', blank=True)
    date_created = models.DateTimeField(auto_now_add=True)
    project_views = models.IntegerField(default=0)
    project_likes = models.IntegerField(default=0)
    project_followers = models.IntegerField(default=0)
    slug = models.SlugField(max_length=100, unique=True) #should this be unique or not?

    def save(self, *args, **kwargs):
        self.slug = slugify(self.title)
        super(UserProject, self).save(*args, **kwargs)

    def __unicode__(self):
        return self.title

class ProjectPost(models.Model):
    project = models.ForeignKey(UserProject)
    title = models.CharField(max_length=100)
    post_overview = models.CharField(max_length=1000)
    date_created = models.DateTimeField(auto_now_add=True)
    post_views = models.IntegerField(default=0)
    post_likes = models.IntegerField(default=0)

表格

#form to add project details
class UserProjectForm(forms.ModelForm):

    class Meta:
        model = UserProject
        fields = ('title', 'project_picture', 'project_overview')

#form to create a post
class ProjectPostForm(forms.ModelForm):

    class Meta:
        model = ProjectPost
        fields = ('title', 'post_overview')

好的,在那种情况下,我会推荐 URL 类似

url(r'^(?P<pk>\d+)/post/add/$', views.NewPostCreateView.as_view(), name='...'),

然后是像

这样的视图
class NewPost(CreateView):
   model = ProjectPost
   form_class = ProjectPostForm
   template_name = 'howdidu/new_post.html'

   def form_valid(self, form):
      self.object = form.save(commit=False)

      # Find project by using the 'pk' in the URL
      project = get_object_or_404(UserProject, pk=self.kwargs['pk'])

      # Then just set the project on the newPost and save()
      self.object.project = project
      self.object.save()

      return super(NewPost, self).form_valid(form)

   def get_success_url(self):
      # Unchanged ...

我在你的代码中看到你试图对用户做一些事情,但我不明白为什么你的 Post 没有用户字段(你可能想添加一个 created_by) 并且 UserProject 应该已经有一个用户集。

我还假设用户首先进入了 his/her 项目,因此您根据定义知道他正在向其添加 post 的项目是他的。如果不是这种情况,则只需更改逻辑以通过常规查询获取 UserProject。例如如果每个用户有一个项目(同样,仅作为示例),则可能使用 `UserProject.objects.get(user = self.request.user)。

无论如何,我在这里做了一些假设,但希望主要问题是如何在新的 Post 上设置项目,这在我的示例中得到了解答。